Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open With... a java program

Tags:

java

file

windows

I want to know how to make a java program that can be used to open stuff up. Ex: notepad++, win zip.... Do I have convert the jar to .exe first? Also, does the file chosen get passed in to String[] args?

By the way, I know that it works with cmd but thats not what I'm asking.

like image 549
Ewen Avatar asked Sep 03 '12 21:09

Ewen


People also ask

How do I open a java file in Windows?

Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type "javac Main.java": C:\Users\ Your Name >javac Main.java

How to make a program in Java?

1 Open a text editor and write the java code for the program. ... 2 Save this file with the .java extension. Make sure that the name of the file should be the same as that of the public class. ... 3 Now, open the command prompt and open the directory in which we have saved our program by using the following command. ... More items...

How do I run a Java program from the command prompt?

Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type "javac Main.java": C:\Users\ Your Name >javac Main.java This will compile your code. If there are no errors in the code, the command prompt will take you to the next line. Now, type "java Main" to run the file:

How do I launch a JAR file in Java?

java –jar example.jar For example, if the JAR file name is “Launch”, edit the .bat file as java –jar Launch.jar. Make sure the file name is correct. Now save the .bat file. From now, every time you open the .bat file, it will launch the JAR file.


3 Answers

Depends on the OS. Under windows, you need to attach some details into the registry.

Have a look at the 3rd answer in Utilising a file association in a Java application for an example?

You could also have a look at http://www.rgagnon.com/javadetails/java-0592.html

UPDATE

Also, when the OS executes the program, you should receive the file as a command line parameter through the main method

I don't know if this will work suit your needs or not, but you could also take a look at File association in Mac

like image 64
MadProgrammer Avatar answered Oct 21 '22 08:10

MadProgrammer


There's many choices on how to make a Java program runnable. Like you mention, the simplest choice is to use the command line. If you want to make it work with most OS's GUI interfaces (and the Open With dialog) the easiest choice is to make an executable jar. IDEs can make this very easy for you, in Eclipse just right-click on the project and select Export > Java > Runnable JAR file.

Another excellent option is to turn your application into a Java Web Start application, which lets users easily run Java programs being served up online.

Alternatively, like you mention, you could convert it into an .exe file:

  • Compiling a java program into an executable
  • How do I create an .exe for a Java program?
  • How can I convert my Java program to an .exe file?
like image 33
dimo414 Avatar answered Oct 21 '22 07:10

dimo414


Deploy the app. using Java Web Start.

JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..

Here is a demo. of the file services in which the app. is associated with the file type .zzz.

..does it get passed via the windows file chooser?

No. It gets passed to the main as either -open filename or -print filename. What the app. does with those strings is up to it. The demo. linked above will prompt the user in the sand-boxed version, simply because it is sand-boxed. The other one should work without showing prompt or dialog.

like image 32
Andrew Thompson Avatar answered Oct 21 '22 08:10

Andrew Thompson