954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Open File With...

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.

mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
 

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?

Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
 

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?

mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
 

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...

Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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?

mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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?

mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
 

> How could I use this in Java though?

public static void main(String[] args) {
    if (args.length > 0) {
        String filePath = args[0];
    }
}
Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
 

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"}

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: