I've been searching for a way to have users set my program as the default to open files for awhile and found nothing. I have a program that is supposed to be universal for Mac, Windows and Linux, so I don't want to use a method that only works with one OS. So how can I give users the ability to set a Java app as the default file opener? Would I use the Desktop class?
No Java does not support this. You would have to write a small app for every OS that you want to support in its native language (c++, objective c, etc.) that would simply launch your java app whenever it is opened. You can then set that app as the default program.
It is answered here on stackoverflow:
Use a Java application as the Default Program for a particular file type?
Quote the answer:
JAR files are normally not executable. That is, a JAR file is not a valid Windows application. It doesn't matter if the JAR extension itself has a default application associated with it, because the "Open verb" is not used recursively in other "Open verb" definitions.
Instead,
Create a batch (".BAT") file (or small EXE wrapper) that calls java (or javaw, as appropriate) and use that executable wrapper as the "Open with" program. (This will have an annoying intermediate console window if using a batch file.) Or,
Modify the registry so that the "Open verb" for the extension launches the JAR through java (or javaw).
In the end, either form should look similar to: javaw -jar TheJarFile.jar "%1%". (Note that javaw is an executable, while TheJarFile.jar is not an executable.)
See java - the Java application launcher for how to use java/javaw.
That is the case for windows. Similarly on Linux and Mac you would have to make a native application wrapper that opens a file, and passes the file's path to your java jar as a parameter and runs the jar.
Related
I created my java application as EXE file, everything works i can open it etc, my problem is that i want to use this EXE as default application for .ctr files, when i try to open that file in Windows Explorer, it doesn't do anything, no log is being created, it looks like it did not even try to open anything.
In the application, I am taking arguments from main method and creating files from it.
How can i even debug to find out what is going on.
I think that the arguments should come by as normal as you would execute app.jar -jar arguments here.
What also strikes me is that if i use terminal to open exe with argument for the file i want it opens and works correctly.
The problem seems to be more associated with Windows in my opinion, I tried to delete file association from registry for the .ctr file and explorer no longer showed any associations, but when i selected open i have seen my app in recommended programs.
This also worked for me for the first time, but then i had to move the app exe files into different directory and it looks like it did not update in windows or something so it may point to the old directory which doesn't exist anymore.
I also tried to write simple .bat file which starts the exe and pass parameter into it, this also doesn't work, it only shows terminal windows for milliseconds and closing it so quick, that i cant see what is going on there.
Launch4j only creates an exe which wraps around your jar files. For file associations, you'll need to edit the registry.
I would recommend using tools that will create and installer or your exe and then also make changes to the registry during installation.
Inno Setup Compiler is a very good tool to create windows installer.
Here is a quick start tutorial.
To create file associations, refer to these answers on SO
inno setup file association
How to change file associations in Inno-setup
I created a tic-tac-toe game in Java. It runs fine on Eclipse.
How do I compile this file (which is currently a .java file) to the standard file format of Java applications, so it can be run from the desktop like a normal program?
What is the standard file type for the final executable Java application? What should be the file type if I want people to easily and without any computer knowledge run my program on their computers?
with eclipse right click on your project. then export it as a runnable .jar file.
Project Right Click > Export > Runnable .jar File.
First choose your project under "Launch configuration", then choose your destination.
After that click finish. Your program should be in your destination folder. Double click to start (just like an .exe file)
For example: If you export it to your desktop, and you name it "TicTacToe", the file on your desktop is "TicTacToe.jar" - ".jar" is your executable file
Done
You have to compile your java class first
javac TicTac.java
and then execute it
java TicTac
Note: that here you provide the name of the class with the main method!
As the other answers indicated, you can create an executable jar using eclipse (or a number of other tools). What these tools are doing under the hood is defining the Main-Class: attribute in the jar's manifest.
In Windows, your users can double click on an executable .jar to launch it, as long as the file associations are configured correctly. However this may not be obvious to windows users who are trained to expect some sort of .exe extension.
To solve this, you could use launch4j to wrap your executable jar in a windows executable. Note: this doesn't change your java application into a native application (it still requires the JVM, etc), it simply makes it launch more like a native application.
For deploying Java desktop apps., the best option is usually to install the app. using Java Web Start. JWS works on Windows, OS X & *nix.
Note that JWS is more effort for us to deploy (it involves not only Jarring and signing the code, but creating a JNLP launch file and a page on the net or network to check that Java is installed & serve the files to the user), but is super easy for the end user.
If there is a JWS deployment, the Jar does not have to be an 'executable Jar' as described in the other answers.
The command should be javac yourFile.java from you command prompt and then after compilation, class file is created. You can run it using java MailClassName
You can find a good tutorial on using javac and java commands here.
I finished a small program. What is the standard file type for the final application written with Java, so it can be run on any computer, easily and without any computer knowledge?
I've been told it's JAR, but Eclipse for example is an .exe file.
What's the standard file type for big, normal applications in Java?
Are most applications distributed in JAR, or rather in .exe or something else?
Serious desktop applications are packaged with platform-specific launchers, which are not written in Java. The launcher must first find out how to run the JVM installed on the system, and then pass it either the path to the executable JAR to run, or the complete classpath along with the name of the main class.
In other words, "it's complicated".
Most desktop applications are distributed using .jar files. A .exe is windows-specific, and non-portable across different operating systems. It's easy to find installers (or "launchers") that will simplify the distribution of a Java program in other platforms, but anyway you'll find that .jar files are the usual packaging mechanism.
If you have a small, simple Java program the easiest approach to distribute it would be to pack it in a .jar, making sure to make it executable. And remember, the computer where your code is expected to run must have installed some version of Java, be it JRE or JDK.
Desktop java applications are usually distributed as jar files.
JRE can launch a runnable jar file using -jar param.
You have one of several options:
1 - Create an executable jar file. By providing information in a manifest within the jar file users can simply execute the jar file by however system-dependent means exist for their OS.
2 - Write a batch file or shell script to invoke the JRE against your jar file (and specify command line parameters for, eg: the main class, the classpath, JVM options, etc.)
3 - Use a tool like jexepack or jsmooth to wrap your Java code within a native executable. I've only ever used these to create Windows binaries - there may be other options for other platforms but shell scripts are typically easier to work with here.
If C# or VB.NET is distributed in .exe files as MSIL codes, what is Java distributed in? Is there an equivalent form of .exe files for Java (other than .jar files, which I don't think is good for distribution on computers). Or is there a way to save Java intermediate language(?) in an exe like MSIL does? Then distribute it in an exe which is still run by the JRE upon execution.
Java "executables" are either .class or .jar files. It's common to deliver a script (.bat file for Windows) with a Java program to run it.
You can use Launch4J to create executable *.exe files from *.jar files, but this is platform specific and using runnable *.jar files is recommended.
For more information on how to make executables with Java please take a look at this thread.
Use Java Web Start to install the app. JWS offers auto-update, splash screens and a lot of other nice functionality.
The user gets desktop shortcuts and menu items if they are requested in the launch file and supported on the OS of the user. Those shortcuts might have icons as supplied by the developer.
What 'jar', what 'exe'? Why show the user either when installing a Java rich client? Using JWS would require Jar files, but the user never sees them.
I've created a program in java and now I want to create an executable from it.
I'm new to java, I don't know if it should be a .exe.
I'we exported my project to a .jar file, but when I double-click it it opens "open with" window.
I want to export my project to a file that runs my program on double-click.
Is there any solution?
Export --> Java --> Runnable Jar file --> Specify the class with static main method to run.
Double click on the Jar file to run..
Thanks...
Java compiliation creates byte code for the JVM, so a native, binary executable is not created during compiliation (like C or C++ programs). This is a feature of Java.
To deploy an application to multiple systems they must have the JRE. These .jar files can be launched from the command line (see this: http://download.oracle.com/javase/tutorial/deployment/jar/run.html)
Some vendors get around this with batch files that launch the JRE to run their application's JAR (and then put these in the start menu, desktop, etc with a fancy icon).
If you want people to install your app (especially from a web page or over a network) you probably want a Java Web Start package (see this for crating one in Eclipse: http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fjava_web_start.htm)
If you just want it to be runnable on your computer, you can use the open with dialog to open it with javaw.exe in JDK_DIRECTORY\bin. Alternatively, if you really wanted to make it an EXE, you could look at some of the answers here.
Either do as in the link mentioned by #dacwe or I would suggest to depending on operating system set a permanent connection between java and jars, then it will always happen.
On Windows you do it by right clicking on any jar then open with and find your javaw.exe in bin folder of your jre installation.
I think you are looking for a simpler approach than Java Web Start.
I assume that you are a Windows User, since you want a .exe file.
So once you have the exported MyProgram.jar file, you only need to create a .bat file
that contains a sole line:
java -jar MyProgram.jar
and place this execute.bat file in the same folder as your MyProgram.jar
This approach works for Linux too, only you need to place it inside a similar file and execute the same command.
Read here http://javabeanz.wordpress.com/2009/01/29/running-an-executable-jar-from-command-line/ for more explanations.