How to create an exe file in java - 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.

Related

Trying to use RXTXcomm.jar with a executable I made

I wrote a program to communicate with an arduino using the RXTXcomm.jar. It works in eclipse because I "Added External Jars" in the edit class path option.
I got the tutorial off this website: http://www.drdobbs.com/jvm/control-an-arduino-from-java/240163864. I originally tried running the command on that website to get the RXTXcomm.jar to work without having to use the eclipse work around but I couldnt get it to work (after much time and research). However, now I want to make an .exe out of my program. I used Launch4J to make an .exe and it works but, like before the eclipse work around, the serial communication aspect doesn't work.
I have thought of two fixes, I do not know the plausibility of either however:
1) Some how export my java gui to a jar file that contains the RXTXcomm.jar that was added when using eclipse work around
2) Write a script that I run once to set the computer up that will allow me to use the RXTXcomm.jar
The main goal is to create this .exe and put all the supporting files in a folder with it, with maybe a setup/readme file to get it working on any PC.
Here is my stackup when I run just the [myprogram].jar file:
java.exe -jar [myprogram].jar
java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while
loading gnu.io.RXTXCommDriver
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no rxtxSe
rial in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
(AND MORE)
I have very little knowledge of what happens behind the scenes of java, I mostly understand the code only. I think that is why I am having trouble with this.
Thank you #hoijui,
I ended up just copying the rxtxSerial.dll to the same directory that the exe is run from and now it works. This directory also includes the RXTXcomm.jar as well. When making the exe in Launch4J I made a custom class path:
The "Main Class" I selected the jar I made from eclipse, then for the class path I added:
echo %CD%\RXTXcomm.jar
and
echo %CD%\rxtxSerial.dll.
I am not sure if this is needed to work but its there and it work so I am not messing with it.
Thanks for the help #hoijui

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.

build java project into executable

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.

Executing Jar File in JDK 1.6 giving issues

I have created a simple project in java using eclipse ide to test the log4j functionality.
I need to run the application from the command prompt using the jar tool in java.
The logtest.java file is simple java file which defines a logger as below.
It is referencing a Logger which is in another Jar file. below is directory structure.
package com;
import org.apache.log4j.Logger;
public class logtest {
private static Logger logger = Logger.getLogger(logtest.class);
public static void main(String[] args) {
System.out.println("---------------HIIIIII-----------");
}
Below is the directory structure that i have created in the project in eclipse.
src/com/logtest.java
lib/log4j-1.2.17.jar
the manifest files entries are as below and i have manually created the maifest file.
Manifest-Version: 1.0
Main-Class: com.logtest
Class-Path: lib/log4j-1.2.17.jar
The contents of the Jar file are as below which i unzipped using the jar utility.
META-INF/MANIFEST.MF
.project
com/logtest.class
manifest.mf
lib/log4j-1.2.17.jar
.classpath
I have exported the jar file and when i try to run using the commond java -jar JarTest2.jar i am getting the following error
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at com.logtest.<clinit>(logtest.java:6)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(Unknown Source)
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)
... 1 more
I know its a class not found exception but strange thing is that the same jar when i create using jdk1.5 and run command tool the jar runs successfully and the java class executes without any error.
I have no idea why this is going as everything is the same.
mine is a 64 bit windows 7 machine and the other machine where it is running is a 32 bit XP machine.
Of course the version of eclipse ide is different on both machines.
Can you please let me know is there something wrong about what i am doing or am i missing something else.
Thanks
Vikeng21
I'm pretty sure that Java doesn't support embedded Jars. I'd recommend moving the lib/log4j-1.2.17.jar file out to the file system (so that it creates a sub folder ./lib within the same drctory as your executable Jar & see if that makes a difference.
If you really want to use embedded Jars, you could take a read of One-Jar
You should have a directory structure of
.\JarTest2.jar
.\lib\log4j-1.2.17.jar
On your file system. The log4j Jar shouldn't be imbedded (contained within) the JarTest2.jar file
Please check your log4j.jar is duplicated. For example one log4j.jar was under lib folder and other was under Referenced libraries of eclipse. If it is duplicated, remove one jar and redeploy again.

I can't load the JDBC driver for MySQL

I've been trying to load the JDBC MySQL connector with the following code:
import java.sql.*;
public class dbTest{
public static void main(String[] args) throws SQLException, ClassNotFoundException
{
Class.forName("com.mysql.jdbc.Driver");
}
}
And I keep getting a class not found exception:
java.lang.ClassNotFoundException
at edu.rice.cs.plt.reflect.PathClassLoader.findClass(PathClassLoader.java:148)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at dbTest.main(dbTest.java:6)
I have added the path to the driver (mysql-connector-java-3.1.14-bin.jar) to my classpath and double checked to make sure it was correct. I also added copies of the jar to the ext folder of my Java installation based on what I read from this article: http://www.developer.com/java/data/jdbc-and-mysql-installation-and-preparation-of-mysql.html
I also searched through posts of others who have had this problem, but all of the responses so far have been saying to add the connector jar to the classpath, which I have already done.
Any help would be greatly appreciated.
I have added the path to the driver
(mysql-connector-java-3.1.14-bin.jar)
to my classpath
The exception tells you that you didn't do it correctly.
How are you setting CLASSPATH? If it's an environment variable, you're going to learn that IDEs and app servers ignore it. Don't use it.
Don't put it in the /ext directory of your Java JDK, either.
The right way to do it depends on how you're using it:
If you're running inside an IDE like Eclipse or IntelliJ, you have to add the JAR to a library.
IF you're running in a command shell, use the -p option for javac.exe when you compile and java.exe when you run.
If you're using it in a web app, you can start by putting it in the WEB-INF/lib directory of your WAR file. If you're using a servlet/JSP engine like Tomcat 6, put it in the Tomcat /lib directory.
On IntelliJ this is how I solved this problem:
File > Project Structure > Libraries > +
Locate the jdbc connector. For me it was on C:\Users\MyName.InteliJIdea13\config\jdbc-drivers
There are two classpaths in java. Build path and run path. Build path is used when compiling .java files into .class files. In a language like C you have a linker stage that fills in all the missing symbols when you run the linker on a bunch of object files. Thats why for .exe(windows) or other native binaries(linux) there is no run path. Java is slightly different because the compiled .class definitions get loaded by the jvm as they are needed.
What the net out of this is that you have to supply a runtime classpath to the jvm. At the command line you use java.exe which searches a few places by default including $CLASSPATH, the current directory/lib, and whatever you supply to the -cp option.
IDEs are different from the command line because they are attempting to shield you from some of the nastiness of running java.exe and supplying the locations where all the .class files are(which would be onerous on a large project).
Most IDE's have some sort of "Run Configuration" tab that allows you to specify certain libraries or locations with classes that will be used when you run your application. Below is how to set the run path in eclipse,netbeans, and intellij.
http://javahowto.blogspot.com/2006/06/set-classpath-in-eclipse-and-netbeans.html
http://www.jetbrains.com/idea/webhelp/run-debug-configuration-application.html

Categories