Creating an executable jar using Eclipse - java

It export it fine as a Jar, but when I double click the project it won't load up.
I'm building a Java Slick2d game (my first). I've tried on both windows 7, and Ubuntu 12.10. Any suggestions?

If you run the exported jar file from the terminal, you'll see an Unsatisfied Link Error. To resolve this, you need to include the LWJGL natives. If you want everything in a self-contained jar, the easiest way is by using JarSplice.
Let's walk through how to do this step-by-step.
Open up JarSplice
Add the jar you exported from Eclipse.
Add the LWJGL natives. These can be downloaded from http://www.lwjgl.org/download.php. I'll be exporting for Linux, so I'm going to include the Linux native files. The native files should be in a folder called native.
Now specify the main class in your project. Don't forget to include the class's package.
Click Create Fat Jar and you're done! You can also export to a Linux .sh, Mac .app, or Windows .exe. Just make sure you include the appropriate natives.
Have fun sharing your game with others!

make sure you mention the Main Class: in META-INF
Detailed instructions
The detailed instructions for creating an executable JAR file for a stand-alone SWT file are listed below.
Create a runtime folder for the desired runtime target on your system (e.g., c:\swt\runtime-linux). Note that the target platform does not need to be the same as your development platform.
Find the correct SWT JAR file for the desired target platform. You can download the desired ZIP file from the SWT website. For example, for Eclipse 3.3 and a target platform of Linux, download the file swt-3.3.1.1-gtk-linux-x86.zip. Expand this ZIP file and copy the swt.jar file to the runtime folder. Remember that this swt.jar file is specific to one platform, in this case Linux.
3.Create a manifest file for your application using the Eclipse text editor (e.g., myapplication-manifest.txt). The text of the manifest should be as follows:
Manifest-Version: 1.0
Class-Path: swt.jar
Main-Class: mypackage.MyClassWithMainMethod
(blank line at end of file)
4.Make sure the manifest file ends with a blank line. Put the name of your package and class that contains the main() method for the Main-Class.
In Eclipse, select File/Export/Java/Jar file and press Next.
On the JAR File Specification dialog, select the source files for the classes you want in the application. In the export destination, browse to the runtime folder and enter in the desired name of the JAR file (e.g., myapplication.jar or myapplication_linux.jar). Press Next.
On the JAR Packaging Options dialog, make sure the "Export class files with compile warnings" box is checked. Otherwise, if your source files have any compile warnings, they will not be included in the JAR file. Press Next.
In the JAR Export dialog, select the option "Use existing manifest from workspace". Browse to the manifest file you created above. Press Finish.
If the JAR file already exists, you will be asked to overwrite it. Select Yes. If your project had any compile warnings, a message will display. If so, press OK.
At this point, the JAR file for your application has been created in the runtime directory.
If needed (i.e., your target platform is different than your development platform), copy the runtime directory to a directory on your target platform.
In your operating system's file explorer, browse to the runtime directory and run your JAR file. For example, in Windows, you can just double-click on it in the Windows File Explorer or, from the "cmd" prompt, you can enter the command: java -jar myapplication.jar. The application should run.

Find file "MANIFEST" in the jar file, and add line :
"Main-Class: {Your executable class name (the class with main method)}"

Related

Running GUI Application without IDE

Lets say that I built a GUI Application using NetBeans. To run this java application I need to open source code in IDE and then run. I know that I can also run through command prompt.
But how do I start the application independent of IDE. Isn't there some .exe file or something like that, which on double clicking directly runs the application?
If not, how do I generate such a file?
Here you can find how to create .jar in Netbeans: How to create a Jar file in Netbeans
You can run the executable jar on every single computer, on one condition - the system have JRE installed.
If you want to, you can also build the .jar using command line, to do that use the following command:
jar cf jar-file input-file(s)
Description from Oracle doc:
The options and arguments used in this command are:
The c option indicates that you want to create a JAR file. The f
option indicates that you want the output to go to a file rather than
to stdout. jar-file is the name that you want the resulting JAR file
to have. You can use any filename for a JAR file. By convention, JAR
filenames are given a .jar extension, though this is not required. The
input-file(s) argument is a space-separated list of one or more files
that you want to include in your JAR file. The input-file(s) argument
can contain the wildcard * symbol. If any of the "input-files" are
directories, the contents of those directories are added to the JAR
archive recursively. The c and f options can appear in either order,
but there must not be any space between them.
This command will generate a compressed JAR file and place it in the
current directory. The command will also generate a default manifest
file for the JAR archive.
After you build your application look for a folder named "dist" in your project's folder. You should find there a file *.jar which can be run anywhere with double click.
STEPS TO FOLLOW:
create a jar
run the jar

How to create an executable file from my project in eclipse

I have a project in eclipse which uses part of another java project for changing some variables. I need to create an executable file from my project but all instructions that I try doesn't work.
I created a runnable jar file by exporting my project but the jar file doesn't run. Then I used the launch4j to create an ".exe" file from the jar file but when I click one the .exe file instead of running, it unzips to a folder.
just in case if it is an important point, I am using mac os.
In Eclipse;
Click Next and give a destination

Create a jar file using compiled class files and an existing MANIFEST.MF file

Is it possible to take existing .class files and a MANIFEST.MF to create a jar file?
Is there a library that can create a "valid" jar-file? I tried it manually and it didn't work (using 7zip).
ERROR: "Invalid or corrupt jar file"
If everything has been compiled before, it should (in my understanding) theoretically work, if you create a new zip file, put all the files in it in the original structure and then rename it to "jar".
My idea is to program something like this with java code. A solution where I could add a file to an existing jar, would also be ok.
If you're interested in why I want to use this, look at my initial question: Compile javacode out of a running java accpilaction - on a system that hasn't JDK installed
Well Jar -cf
Try the jar command in $JAVA_HOME/bin
$JAVA_HOME is the path to you JRE/JDK installation

How to run Java programs by clicking on their icon on Windows?

I have written a Java program that uses Java swing library. Now I would like to execute this program by double clicking on the executable file on Windows just like any other program with a GUI. How do I do that?
Since it is Java based and has a GUI, the obvious answer is to deploy it 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..
By 'desktop integration' read desktop shortcuts and menu items on supported platforms.
The 2 icons on the right (JotPad & Star Zoom Animation) are both Java based apps., installed using Java Web Start. Since JotPad is sand-boxed, the user will be prompted as to whether to create the shortcut. That choice is not offered for apps. with higher permission levels, so it would make more sense to install/remove the shortcuts and menu items using the IntegrationService - which allows an app. (after prompting the user) to create/remove them at run-time.
There are number of options:
Create an executable jar of your project. for this jar to work you have to have javaw as default application to open it.
Create an exe of your project.
Create a bat file which runs your jar file.
Take a look at this: How can I convert my Java program to an .exe file?
While the others mention excellent choices like creating a native executable, there is another useful method: creating a shortcut.
Right click your desktop, expand the "New" option, and click on "Shortcut".
Type "javaw.exe". Click next.
Name it whatever you want. Click done.
You'll notice the newly created shortcut on your desktop. Right click it and choose "Properties"
In the "Target" textfield, append "-jar path-to-your-jar.jar" where you replace "path-to-your-jar.jar" with the actual path to your jar
You can also now optionally change the icon to whatever icon you want
This shortcut can be pinned to the taskbar and be used from anywhere (considering you provided an absolute path to your JAR).
you need to create exe from the java program.
Creating executable jar files
First, make sure you have installed Java 1.2 or above. This facility is not available in previous versions of Java.
Next, create your working java system. In general, you will want to put it into a package. For this example, I created a trivial HelloWorld application that prints out "Hello World" plus the first command line argument, and placed it into the package "psae". Therefore, the HelloWorld files (HelloWorld.class, HelloWorld.java) were located in the directory psae. I tested the system to make sure it worked before going on to the next step.
In the directory in which the psae is located, created a file called "mainClass". This file contains a single line specifying where the main Class is to be found in the jar file. Note that I use the package specification. Here is the single line:
Main-Class: psae.HelloWorld
Note: make sure you type a carriage return after this line; some windows systems need it and will report a "Failed to load Main-Class manifest attribute" error.
Next, I create a jar file called psae.jar using the "jar" command in Java2. I use the "m" command line argument to specify the manifest file mainClass, which adds information to the jar file on where the main class will be found. Here is the jar command:
bertha:~ > jar cmf mainClass psae.jar psae
Just for fun, and to check what's happened, I print the table of contents for the jar file I just created. Here's the command and its result:
bertha:~ > jar tf psae.jar
META-INF/
META-INF/MANIFEST.MF
psae/
psae/HelloWorld.java
psae/HelloWorld.class
Having successfully created the jar file, I can now invoke java2 on it with the command line argument:
bertha:~ > java -jar psae.jar Philip
Hello World Philip
There are a few projects, like http://jsmooth.sourceforge.net/ and http://launch4j.sourceforge.net/
you can use something like Launch4j.
also look at JSMooth.
Hope it helps
There are two ways. Both involve packaging your code in a .jar.
The first way is to build an actual .exe file using a tool like Launch4j. It will require you to set up things like tell it which class to execute, which icon to use, which JRE is OK, what JRE parameters to use, etc.
The second option is to make the .jar itself executable. You do this by adding a manifest to the .jar. The manifest is a small configuration file that describes the jar. One of the attributes is Main-Class which defines the entry point. In other words, it says which class has the main function that should be called when the user double-clicks the file.
Here's a basic tutorial about manifests: http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html
The 2nd option is easier to get going, but users will know what to do with a .exe far more often.
Note that if either approach complains that it can't find the class, make sure to set the classpath manifest attribute to match your project.
If you have an executable jar file, just shift-right click on your file and set it to be opened by javaw. The other option (in case you want to pass in parameters to your application) is to create a .bat file where you spin off your application via java or javaw
Right click to your "project" in eclipse and select "export" then choose "Java->Runnable Jar File" select your project name and finish.
Seems you want to deploy and run the standalone application of swings. Being a java developer you should understand the power of jar files. Those are executable in themselves {so no need to create .exe files :)} .
The below code will help you to create a jar file.
Creating a jar File in Command Prompt
Start Command Prompt.
Navigate to the folder that holds your class files:
C:\>cd \mywork
Set path to include JDK’s bin. For example:
C:\mywork> path c:\Program Files\Java\jdk1.5.0_09\bin;%path%
Compile your class(es):
C:\mywork> javac *.java
Create a manifest file:
C:\mywork> echo Main-Class: NameOfProject >manifest.txt
Create a jar file:
C:\mywork> jar cvfm NameOfProject.jar manifest.txt *.class
Test your jar:
C:\mywork> DanceStudio.jar
After creating a jar just double click on it and you are done.
You have to create an executable jar file. For that you just simply add a META-INF folder to the jar, then add a MANIFEST.MF text file with two lines:
Manifest-Version: 1.0
Main-Class: your.package.YourMainClass
Here's how to run a Java program by RIGHT-CLICKING on it (in other words, from the Windows Explorer context menu). This handy trick is great for beginners who need to test their simple programs on the fly. Works on both Win7 and XP rigs.
[ATTN: Depending on the situation, you may need to remove the package directive from the top of your Java file.]
Step 1. Create a batch file (e.g., RWJ.bat) inside a folder of your choice (say, in C:\Program Files\Java.)
Step 2. Fill RWJ.bat with the following commands (they will work just fine as is with simple classes but you can, of course, tweak them according to your particular needs by specifying compiler / interpreter switches, passing args, adding echo off, removing pause or whatever):
javac %1
java %~n1
pause
The first command passes the full name of your right-clicked file to the Java compiler; the second one strips the file extension and feeds JVM with the class name only.
Step 3. Add the following key to your Registry: HKEY_CLASSES_ROOT\*\shell\Run With Java and then create its command (default value):
C:\Program Files\Java\RWJ.bat %1.
Step 4. Run your Java class by right-clicking it and selecting Run with Java option.
That's all there is to it.
Another way to run Java programs by pointing and clicking is to use AOT compilers. For example, GCC has an entry point named GCJ, which can be used to compile the source code into both byte codes and standard executable file for your particular OS.
And finally, instead ot batch files one can run WSH, etc.

Load files External To The Distribution Jar

I have written a Java program which I package and run from a JAR file. I need to have some user-changeable configuration files which are simply text lines of:
key = value
format. To load these files I used the class described here. When I run my program through Netbeans IDE all works fine as I have included the directory where I store the configuration files in the Project properties.
The problem comes when I build my application into a JAR file. As I want the configuration files to be user-editable I keep them OUTSIDE of the JAR but in the same directory but now when I run my application from the command line it cannot find the configuration files. If I manually add the files to JAR file at the ROOT folder then all is well.
So how can I tell Java to look outside of the JAR for my loadable files? The -classpath option has no effect.
That's because the way you are loading them requires that they be inside the .jar when running from a jar, or inside the project directory if not; it's relying on the classloader to tell it where to find the file.
If you want to open a file outside the .jar, you need to just open it as a File and read it in.
One of the ways we've approached this is to take the external filename as an option on the command line (e.g. java -jar myJar.jar -f filename). This allows you to explicitly state where the file is located. You can then decide whether or not to also look in a default location, or inside the .jar if the file isn't specified on the command line.
I resolved it by referring to this question. I Added the current directory to the MANIFEST file of the jar and it works.
Why is the -classpath option ignored in this case I wonder? Security?
I had the same problem and saw your post, but the answer in the end, was simple.
I have an application deployed via Java Webstart and am building it in Netbeans 7.3.
I have a properties file config.xml that will be updated during run time with user preferences, for instance, "remember my password".
Hence it needs to be external to the jar file.
Netbeans creates a 'dist' folder under the project folder. This folder contains the project jar file and jnlp file. I copied over the config.xml to the dist folder and the properties file was loaded using standard
FileInputStream in = new FileInputStream("config.xml");
testData.loadFromXML(in);
in.close();

Categories

Resources