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.

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.