How to open all files extensions in java? [duplicate] - java

I am displaying a list of files; i.e. xls, doc, pdf, odt etc., in my Java application (Eclipse RCP). When the user clicks on the file, I want to launch the appropriate (according to what the OS thinks) native application, just like it happens in Windows Explorer or the Finder.
And while I am here: It would be nice to also display the same icons that Finder or Explorer use for the different file types.
Is there a library or Eclipse plugin for this?

What you want is java.awt.Desktop:
Desktop.getDesktop().open( file );

I have found an API in Eclipse's SWT now that seems to do the trick:
org.eclipse.swt.program.Program "provides access to facilities for discovering operating system specific aspects of external program launching."
It has methods to find the program for a given file extension, get the program's icon, and even launch the program.

Sounds like you're after the Java Activation Framework ("JAF"). This API lets you determine what files are and what actions you can do on them. Or alternatively the Java Desktop Integration Component ("JDIC"). JDIC allows you to create and no doubt query file associations.
Both projects seem to be in a semi-abandoned state howeer (sigh). But that's par for the course for Sun these days. Only other thing I know of is some Windows specific third party library that's based on JNI called Winpack. It does a bunch of other things too.
You can get the associated icon using the FileSystemView class (Java 1.4+).

Related

How to wrap a Java Web Start in .exe?

I'm afraid that many users still don't know what Java Web Start, so they may get confused by the small, single .jnlp file. So is it possible to wrap it as a very normal application, i.e. Windows .exe(or OSX .app) with pretty icon?
"The shortcut element can be used to indicate an application's preferences for desktop integration." The desktop element in particular allows platform-specific integration in a way that users expect.
I wrote a script to fix-up the file association for executable .jar files. You could just modify my script and that would give your users a way to register the .jnlp extension if it wasn't yet registered on their system.
NOTE: In the case where you have a user who hasn't installed Java on their system, they wouldn't have this file association, and you could use this script to associate a "bundled jre" with .jnlp extensions without the user needing to install Java.
Basically JAVA runs on a virtual machine, but cross-compilers can be used to re-compile your java code , creating an .exe or .app file
if javafx is an option, linked article describes nice ways how to deploy the same application both as .exe and .jnlp
http://docs.oracle.com/javafx/2/deployment/self-contained-packaging.htm#A1324980

Associate file type with Java Swing application

I am creating a java swing application and I need to set the my program as the default program for the file extension .mcsd (MIME type text/mcsd). This must work on windows, and it would be nice if it worked on OS X/Linux as well. I am somewhat new to java (3 or 4 months) so please don't bombard me with all kinds of expert talk.
I have seen associating a custom file extension with java app in windows, but I would prefer not to use the Java Web Start. If possible, I would also like to know how to set the icon of that file type. I have found exe4j as a way of creating the .exe and plan to use it, but if is not the best tool for convertinf to an exe or someone knows something better feel free to tell me. Another thing: I need to be able to get the data from this file after the program has opened.
So my question is: how do I do this?
Deploy the app. with Java Web Start:
JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update ..
See the file services demo. for an example of a JNLP that declares an interest in the .zzz file type.
This is how the association is declared in the JNLP:
<association
extensions="zzz"
mime-type="text/sleepytime" />
JWS is supplied by Oracle and works on Windows, *nix & OS X.
..but I would prefer not to use the Java Web Start. Because I would like to have a native installer that just installs the program, without opening a web page or anything. If that's not how (web start) works, then I'm happy to use it.
JWS works over the web or a network. But if you can invoke the opening of an URL (to the JNLP) from the desktop, there does not need to be any browser opened.
Would I do that using openConnection() on a URL object?
I would use the Desktop or BasicService API.
Desktop.getDesktop().browse(uri);
boolean result = basicService.showDocument(url);
As noted in the BasicService demo.
In Java 6+, a call to show another web start launch file (e.g. BasicService.showDocument(another.jnlp)) will be handed directly to JavaWS, with no browser window appearing.
I expect this has always been true of the Desktop API, given the JWS client is the default consumer for that file type.

How to associate a file with a program in java

Take notepad for example. You can open a file with notepad (right click and open with, or double clicking on the file if it is already associated with notepad) and it will display the contents of a file. I want to be able to open an xml file with the program that I am writing so that it can do some parsing and give a desired output. What sort of ways are there to do this in java? Do I have to ask for an argument to be passed into the program when it is opened? Would this be easier in C++ or some other language? If you need any further information please let me know -- this was the best way I could come up with to discribe my situation, which may be why my google searches have come up empty. Thank you in advance for your help!
You can associate an extension with a java jar and the file name will be passed as args[0] It doesn't get much simpler than that. I suggest you try it with a "hello world" program. ;)
display the contents of a file ..What sort of ways are there to do this in java?
Install/launch the app. using Java Web Start.
Java Web Start (JWS) is the Oracle Corporation technology used to launch rich client (Swing, AWT, SWT) desktop applications directly from a network or internet link. It offers 'one click' installation for platforms that support Java.
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 JNLP API file services (an API available to JWS apps.) that registers an interest (claims an association with) in the .zzz file-type in the launch file. Associations can also be done programmatically using the IntegrationService of the JNLP API.

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

Distribution of Swing application

I want to distribute a swing app to my client. In that application two property file are there, which should be visible to client so that they can edit, I can not give it as runnable jar. Also I want something like apache-tomcat zip file which once you extract it will arrange folder structure also will give property file to edit and on next run it reads that property. Also apache-tomcat starts with startup.bat or startup.sh like that I also want.
Use Java Web Start to install and update the application.
User editable properties
Put them in (a properties file in) the Jar
When you go to check for the properties, do so using the PersistenceService. If they do not exist, read them from the text file in the Jar, and put them there. Here is a demo. of the service.
When the user wants to see/edit the properties, show them via a JOptionPane.
Desktop integration
JWS offers desktop shortcuts and menu items to launch apps., when supported by the platform, as well as an API to install and remove them (see the IntegrationService). Much slicker than batch files!
Currently we are using GetDown to handle distribution of our swing applications. We use Tomcat to distribute updates and GetDown to download those updates. It's really flexible and powerful, and much better than java WebStart.
A good tutorial : http://www.hascode.com/2012/05/creating-updatable-java-applications-using-getdown-and-the-getdown-maven-plugin/
Project website : http://code.google.com/p/getdown/
If I understand you correctly you want a tool to make distribution package for your application. That generates folder structure and unpacks application and data files to this structure.
There are many free and commercial setup builders. I think you would prefer multi-platform builder, so look for java setup builder.
Check this thread, this question was discussed there.

Categories

Resources