简体   繁体   中英

Open With… a java program

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.

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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM