Hey people,
How could I make it so when I right click and file and click 'Open With..' and choose my program so it loads the file?
I already know how to load the file from when the program is already running and you click 'load' ect...
But,
I tried using the 'Open With..' menu and it doesn't load the file, obviously.
So.. How could I make it work with that?
Thanks!

P.S I've tried Googling and looking in the Java API/docs but I can't find what I'm looking for, maybe I'm just blind.

Dani AI

Generated

As and started to point out, the real work here is OS integration: the OS is what decides what gets launched when someone chooses "Open with…". For quick testing a wrapper script/exe is fine, but for a polished editor you should register your file types so the OS launches your program for those extensions (installers or packaging tools are the right place to do that). (docs.oracle.com)

On Windows that registration lives under the file‑association/ProgID keys (HKEY_CLASSES_ROOT and the Default Programs registration). Installers normally create a ProgID and the shell\open\command value so Explorer will show your app in "Open with…" and start it correctly; make the installer write those entries (or use the Windows Default Programs APIs) and be careful to quote paths when there are spaces. (learn.microsoft.com)

On Linux desktop environments you create a .desktop file and list supported MIME types. Put your launcher in Exec and use the field codes (%f for a single file, %F for multiple files) so the DE will pass filenames correctly. Example .desktop snippet:

[Desktop Entry]
Type=Application
Name=MyEditor
Exec=/opt/myeditor/bin/myeditor %F
MimeType=text/plain;application/x-mytype;

Refer to the Desktop Entry spec for details. (specifications.freedesktop.org)

On macOS a Java app must be a proper .app bundle with CFBundleDocumentTypes entries in Info.plist for the system to hand it files; once packaged, use the Java Desktop API’s OpenFilesHandler (available since Java 9) to receive files when the app is launched or when it’s already running. A short handler looks like:

Desktop.getDesktop().setOpenFileHandler(e -> {
    e.getFiles().forEach(f -> openFile(f));
});

Check the Java Desktop docs and Apple bundle docs when building your installer/bundle. (docs.oracle.com)

Quick tips: test both "app already running" and "double‑click to launch" flows, ensure your installer registers associations (tools like jpackage can automate this), and always handle quoted paths and multiple files. (docs.oracle.com)

Recommended Answers

All 11 Replies

Eh??? I am not sure what you mean here??? Also, why would it relate to Java??? Could you please explain again about this??? Are you saying you are attempting to open a java file to see its source code? Or you want to run a java class?

An example:
Right click a text file and click 'Open File With...' or something like that in the context menu.
Then click on notepad and notepad runs and automatically loads the text file.
So how could I do this to, let's say I created my own text editor and wanted to do the same like notepad.
So how could I do this with Java?

I have a feeling that this is not a Java question... Anyway, have you tried to select 'other application' and choose it to run? I am not sure how Window passes the file as an argument to the Open With...

You could create a batch file that passes the first arg along as an arg to your program:

java -jar MyProgram.jar %1

If you select your batch file in the "Open with..." dialog, the filename will be passed as that argument %1.

I've tried to select my application and chose it to run but it doesn't load the selected file.
So there's no way to do this in Java? Will the use always have to launch the program first then load the file?
Why do you get the feeling that this is not a Java question?

I don't understand, how could I get the %1 and use it?

The %1 in my example above is the first parameter to the bat file, which in the case of "Open with..." it will be the full path of the file.

You are pass that as an arg to your Java program, so you can pick it up in the args array in main(String[] args) .

>Why do you get the feeling that this is not a Java question?
Because it isn't really, it's an OS question.

So %1 will be the full path of the file? How could I use this in Java though?

I guess this is a Windows question, should I make a thread in the Windows forum?

> How could I use this in Java though?

public static void main(String[] args) {
    if (args.length > 0) {
        String filePath = args[0];
    }
}

Wait you mean, that (String[] args) is collected from the outside?! That's so cooool!
This has fixed more than one thing now, thanks!

Yes, that is the entire point of the argument array to main(). If you execute your class/jar with the following statement

java MyProgram A B C

your args array in main() will contain {"A","B","C"}

commented: Because I Love you for helping me! +1

It seems I still have alot to learn about Java :S
Well thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.