build java project into executable - java

How can i make an executable of my project in Java?
I tried to right click on my project and selected export .
the problem is that the exported jar file wont open when I execute it! Have I missed something? And is there a way to make an .exe executable from my project?
when I execute the jar file in cmd it says :
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.libr
ary.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.lwjgl.Sys$1.run(Sys.java:73)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
at org.lwjgl.Sys.loadLibrary(Sys.java:95)
at org.lwjgl.Sys.<clinit>(Sys.java:112)
at org.lwjgl.opengl.Display.<clinit>(Display.java:135)
at org.newdawn.slick.AppGameContainer$1.run(AppGameContainer.java:39)
at java.security.AccessController.doPrivileged(Native Method)
at org.newdawn.slick.AppGameContainer.<clinit>(AppGameContainer.java:36)
at JavaGame.Game.main(Game.java:34)

UnsatisfiedLinkError indicates that you're missing a native library (usually a .dll file on Windows, or .so file on Linux). You'll need to do one of the following:
Not recommended: Copy the native library to a location on the default java.library.path (on Windows, this includes C:\Windows\system32)
Not recommended: Copy the native library to a directory, then run your program with java -Djava.library.path=dir/containing/library -jar <jarfile>
Recommended: Bundle the DLL in your jarfile, then modify your code to extract the DLL to a temporary directory and load it using System.load or System.loadLibrary.
You can use either of the first two solutions above as a quick hack to get it working, but neither of those solutions is very good. The best solution from a deployment standpoint is #3 above.
In your case, you're using the Lightweight Java Game Library, or lwjgl as referenced in your UnsatisfiedLinkError. So you'll need to include any DLL(s) that come with lwjgl.
When you unzip lwjgl, you'll notice that it has a native directory with a subdirectory for each supported platform. Here is a listing of lwjgl's Windows DLLs:
To implement solution #3 above and make your executable jarfile cross-platform:
in your project/jarfile, create a separate directory for each platform
put all the native libraries for each platform in the appropriate directory (it may be helpful to put them in the same directory as some utility class that you'll later use to extract them)
when you export your program to a jarfile, make sure the native libraries are included
look up the platform/operating system (e.g., System.getProperty("os.name"))
in your Java code (probably in your main method or some utility method), create a temporary directory
for whatever platform you looked up in step 3, extract the appropriate native libraries into the directory you created in step 4 (hint: use Class.getResourceAsStream to get an InputStream, then use Files.copy to extract it to a file)
for each library you extract in step 5, call System.load("path/to/library_file")
See https://stackoverflow.com/a/1611367/44737 for a nice example including code.

You can either package everything in a jar manually using the jar command line tool or you can automate the process of packaging your jar using tools like Ant, Maven or Gradle.
Once you have your class files properly packaged in jar file, you can execute them by creating a script file appropriate for your operation system. For instance a batch file in windows or a bash file in Linux.
All you have to do is invoke the java command and provide your class path and the name of your application entry point.
#!/bin/bash
java -cp myApp.jar com.my.app.Main
And that's it. You execute your application by invoking that script.
Additionally, you can make arrangements to create an executable jar, by means of defining a MANIFEST file for you jar file. In that manifest file you can place a property Main-Class that indicates your application entry point and you can define another property called Class-Path which allows you to specify a list of other jars needed by your application.
See Running Jar-Packaged Software.
If you package your jar this way, some operating systems allows you to execute the application by simply double-clicking the jar.
But most probably, you still will need to create a script, just a bit different this time:
#!/bin/bash
java -jar myApp.jar
See the Java Tutorial: Packaging Applications in Jar Files.
Ultimately, if you really, really need to create an executable file, and if you're working on windows, you may consider a tool like WinRun4j.

Related

Exporting a runnable jar, opening it returns an UnsatisfiedLinkError

I currently wrote a simple GUI in Eclipse which runs as intended. I was hoping to export it so I can share it with my friend (who doesn't need to install eclipse and the java libraries). I tried all 3 library handling method Eclipse provides and none of them works. I read a little online and saw something about a manifest file, but wasn't quite sure what to do with it. Is it going to help?
This is where I placed the folder that comes with the .dll file.
This is the result. Am I doing something wrong?
As indicated by the error messages in the first screenshot, what you are missing here is the native library - the software library written and compiled to native code specific to the operating system. What you will need to do is provide the libraries specific to the operating system on which your software will run, eg. dlls for 32 or 64 bit Windows. The manifest does not provide the capability to include those libraries.
When the program is run on Windows, Java will look for native libraries in the following locations:
The current directory
The directories in the PATH environment variable
The directories in java.library.path (if it's specified)
It may be easiest to simply put all files in the one directory. If you do this, you should be able to run the program in the same way as you do now.
The java.library.path option is only needed if you want to put your native library files in a directory separate to the one in which you run your program and not on your PATH. It is only in this case that you will need to add java.library.path, eg. by adding -Djava.library.path=c:\path\to\your\lib after java. Also note that you may use a relative path, ie. a path that is relative to the directory you are in when you execute the command.
I also see from your later error messages that you have another dependency, but on a java library LeapJava.jar. As running a jar with -jar will only work if you have a single jar, but because you have more than one (your own program plus the dependency), you'll instead need to use the -classpath (or -cp for short) argument and add your main class. The classpath argument is a semicolon-separated list of classpath locations, while the main class is the one containing your public static void main method, eg. your.package.name.YourMainClass. So assuming your UI.jar is still in C:\Users\Ian\Desktop\Leap Data UI, you should be able to navigate to that directory and execute with:
java -cp UI.jar;UI_lib\LeapJava.jar -Djava.library.path="UI_lib\x64" your.package.name.YourMainClass

Providing path relative to main class for classpaths when running from command line

I am trying to execute a compiled version of my program attachmentUploader.jar from the command line. This has a number of dependencies on various Java libraries, which I am planning to bundle up with the program and specify as part of the classpath. This package will be moved between environments, and will be executed using the Java runtime for that environment. Using absolute paths for my environment, the command looks like this:
"C:\Program Files\Java\jdk1.6.0_37bin\java" -cp "C:/Users/My Documents/jars/attachmentLoader.jar";"C:\Dev Tools\jxl.jar";"C:\Dev Tools\org.apache.commons.codec";"C:\More Dev Tools\java-json.jar" com.custom.test.postClient
I would like to amend all the classpath jar paths to be relative to where my main class is saved, but I read that relative paths in this situation would be relative to where the Java is executed from. In my case this could be anywhere, as it will depend on where this has been installed. Is there a straightforward way of getting round this?
Thanks all.
There are actually several ways:
Make your jar file an executable jar file. That is done by adding a manifest file to the jar specifying what the main class is, and where the jar dependencies are located, relative to the location of the main jar file. The command to execute the program would then be java -jar path\to\jarfile.jar
Use a wrapper executable or shell script, that uses the location of the script itself to compose the classpath. Such a script would depend on the system (Unix/Windows), but both platforms allow script files to know their own location, and thus to use that location as a basis for the location of jar files. An advantage of that technique is that it also allows passing system properties, memory options, etc. to the JVM.
I recommend you use a standard build tool such as gradle, which has an application plugin generating all those script files for you, for all platforms, and bundles the whole application into a zip or tar.gz file.

When I package my project into a .jar with eclipse, it can't find lwjgl [duplicate]

I'm making a basic game in Java using the LWJGL Library via Netbeans.
I've created a library with the lwjgl, lwjgl_util, and jinput .jar's, and I added -Djava.library.path=C:\LWJGL\native\windows to the "Run" category in the project's properties.
When I run the file in Netbeans, it runs perfectly with no issue. But when I run the .jar via double-clicking the file, nothing pops up (not even the momentary cmd error window, as far as I can tell). And when I run the file via command line, I get:
C:\Users\200160765>java -jar "C:\Users\200160765\Documents\NetBeansProjects\Game
\dist\Game.jar"
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.libr
ary.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.lwjgl.Sys$1.run(Sys.java:73)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
at org.lwjgl.Sys.loadLibrary(Sys.java:82)
at org.lwjgl.Sys.<clinit>(Sys.java:99)
at org.lwjgl.opengl.Display.<clinit>(Display.java:130)
at game.Draw.createWindow(Draw.java:198)
at game.Draw.init(Draw.java:214)
at game.Draw.run(Draw.java:56)
at game.Main.main(Main.java:9)
I've tried moving the DLL's and .jar library files around to the 'lib' folder in the same directory as Game.jar, and moving them to the same directory as Game.jar, but I get the same error. Could someone help me as to why I can't seem to get this working outside of netbeans?
you have to point the jvm to where the native files are located using a command line parameter -Djava.library.path="path/to/natives". You could use a batch (.bat) file to specify this and start your application for you.
Alternatively you can use a tool like JarSplice to create a single executable jar file from all your jars and at the same time include your native files inside it. It automates the tricky part of specifying the natives manually and provides a nicer end user experience.
To use JarSplice just select your game.jar, lwjgl.jar, lwjgl_util.jar, and jinput.jar in the jars tab. Then all the *.dll, *.so, *.dylib and *.jnilib files in the natives tab. Add your main class on the class tab and create the single executable jar.
LWJGL needs the native components for your particular platform to be in java.library.path. These are in the subdirectory native in the LWJGL distribution and end in .so on Linux, OSX and Solaris and .dll for windows.
When I had this issue, it was because i accidentally put the argument to specify the location of the natives (-Djava.library.path=/native/) in the field called 'Arguments' under the run category of the options panel, instead of 'vm Options'.
As seen here: http://s30.postimg.org/6f90akidt/Capture.png
And yet another way to do this is with Java Web Start (jnlp): http://lwjgl.org/forum/index.php?topic=3763.0
This makes sharing your project easier in some ways.
I had this problem and fixed it using jarSplice (http://ninjacave.com/jarsplice)
make sure you delete all of the preplaced natives in your jar before you create the fat jar, otherwise it will create a duplicate error
I also got the same error and then realised that I named the file "my_lib.zip" instead of "my_lib.jar". Maybe it may help someone.
Another thing to check:
If you are using a 32 bit JVM, you need 32 bit libraries. (Even on a 64 bit OS)
If you are using a 64 bit JVM, you need 64 bit libraries.

JAVA, Batch file errors

I have an existing Java project that compiles and runs properly through Eclipse. I have created the following .bat file to run the program sans Eclipse:
java -classpath jflashplayer.jar;bin TestProgram
The file is saved within the project folder, but not within the bin folder (located in same directory as bin). When I try to run the batch, I am met with a large number of runtime errors, the first being
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils
I'm not sure why I get this error when it compiles and runs properly via Eclipse. I have the commons-io jar files linked to the project within Eclipse as libraries, and the jar files are themselves located in the project file (same directory as the batch file and the bin folder).
Also, I'm not entirely sure what the -classpath jflashplayer.jar bit of the batch file is doing. I am using the jflashlayer.jar library (also linked to the project within Eclipse and in the same location as the other jar files), but I am not sure why it would appear in the batch file. I edited an existing batch file from a similar project that uses the jflashplayer.jar files, and it has worked previously to leave that part in.
When I write code in Java, I rarely require it to compile/run outside of the IDE, so I usually have troubles when it comes to this part. Perhaps there is a more robust and foolproof method to run the program outside of the IDE other than the batch file method.
The batch file method is fine, but you have to specify all the libraries you're using on the classpath, just like the jflashplayer.jar.
In this case, the error you're getting is because the Apache commons-io library is not specified on the classpath. Your command would have to look something like:
java -classpath jflashplayer.jar;commons-io.jar;<other jars ...>;bin TestProgram
Alternatively, you can create a runnable jar from Eclipse as described here. When you select a library handling strategy, choose the option Extract required libraries into generated JAR. This will make it so that all the library classes you're using are packaged into your application's jar file, and you can just execute it by invoking java -jar my_application.jar.

How to create an exe file in java

Hi I want to create an exe file for my java app.
I tried with some third party softwares JEXECreator, successfully created the exe file and its working fine in my system, when I tried with another machine, it’s not working. I got the following error
* The error occurred while running the application. The exit code is 0x10000223.
* Contact the vendor of the application for troubleshooting.
java.lang.ClassNotFoundException: com.sample.SampleMain
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.ucware.JEXEClassLoader.run(Unknown Source)
at com.ucware.JEXEClassLoader.main(Unknown Source)
**************************************
I know there is something wrong with the classpath which I set.
Actually I want to create the exe file myself without using any third party software.
I found the steps in lot of sites
Created the manifest file named Sample.mft with following contents
Manifest-Version: 1.0
Main-Class:
Class-path:
In this I have some doubts,
How the Main-Class should be added, with the full package name (com.sample.SampleMain) or the class name alone (SampleMain) or with the extension (SampleMain.class)
How the class-path should be added, I have 4 java classes and 2 jars in my project. How to add all these in the class path, and do I need to add the java jdk in classpath.
Where the manifest file should be saved
What should be the manifest file extension (mf or mft)
In command prompt from which directory I should create the exe file (from my project folder or src folder or the folder which contains all the java classes)
What’s the syntax should be used while creating jar in command prompt
(jar cmf Sample.mf Sample.jar Sample1.class Sample2.class Sample3.class Sample4.class jar1.jar jar2.jar) like this or (jar cvfm Sample.jar sample.mf *.class)
When I did something like this I am getting a jar instead of exe file, When I run the jar in command prompt using "java -jar sample.jar" then I am getting class not found exception".
Actually how to create an exe file instead of jar file, that means just by double clicking that exe file, should run my app in any machine.
Can anyone help me to do this?
Thanks in advance.
I use the Ant tool under Eclipse IDE to work with InnoSetup and Launch4J to create the EXE and its installer which it also manages the classpath...
A guide? You can refer to:
http://www.eteks.com/tips/tipCreationExe.html (in French)
Can't speak for JEXECreator, but I can recommend JSmooth (http://jsmooth.sourceforge.net/). I've successfully used it for several projects (e.g. this SWT based Java app).
Personally, I like JSmooth. That is just wrapping. This means it is still a Java application. When executing the exe, it will unpack the jar to a temporary folder and then execute it with javaw -jar ...
A second option is gcj. But that is absolutely a bad choice. That doesn't wrap the jar in an exe, but it really compiles it to native system code. But this slows down your application very much. You can check some results of my timing on this topic.
I had some positive experience with Excelsior JET. Unlike gcj it actually works and execution times are faster than that of an executed .jar file. The downside is that it's not for free.
Well I will recommend you to create one bash file instead of doing complex things and by double clicking on it you can run your application.Yeah but you cant change its icon but there are many free tools are available by using which you can easily convert bash file to exe.
To create an exe file, I use launch4j. Launch4j converts the jar file into exe file and if I wanna pack it for the installer, I use InnoSetup. For me, that exe created by launch4j works on pcs.

Categories