Exporting JAR from Eclipse - java

I've created a SWT application using eclipse. In the application I make use of Images with in the project. Now I build the jar file and run it in the command line I get the following error:
Caused by: java.io.FileNotFoundException: Images\Back.png (The system cannot fin
d the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at org.eclipse.swt.internal.Compatibility.newFileInputStream(Unknown Sou
rce)
Now I can see that the program cant read my Image but I would like to ask how to I include the Images in the jar file for everything to run smoothly.

Right click on your project, choose export.
Export -> Java -> Jar file
Choose the resources you would like to export, then click Finish.
If you need a runnable jar, just make sure the files are part of your project. For example make a folder called "Images". Then exporting a Runnable Jar should work.
That will get you the Images into your Jar file. Once it's there you will need to retrieve it using the method "getResourceAsStream". If you need help with that there are numerous good answers, here is one, Getting images from a .jar file.

Related

FileNotFoundException error when using a runnable Jar File in Eclipse

I am working on an assignment that involves the use of 2 txt files and creating a runnable jar file. Part of the assignment involves creating a runnable jar file called Hw2.jar, exporting that file into the "first directory level", then going into run configurations in the arguments tab and pasting, "java -jar hw2.jar ct.search ct.corpus" (the ct.search and ct.corpus files are the txt files) Here is what it looks like.
Yet despite having completed the steps, I'm getting a FileNotFoundException Error, as detailed below.
Exception in thread "main" java.io.FileNotFoundException: -jar (The
system cannot find the file specified) at
java.io.FileInputStream.open0(Native Method) at
java.io.FileInputStream.open(Unknown Source) at
java.io.FileInputStream.(Unknown Source) at
java.io.FileInputStream.(Unknown Source) at
java.io.FileReader.(Unknown Source) at
edu.iastate.cs228.hw2.Console.main(Console.java:29)
This is my first time trying to use a runnable jar file within the run configurations, so I am unfamiliar with how exactly the process works. I would very much appreciate any help with this issue. Thank you.
EDIT: The jar and txt files are NOT in the src folder. They are inside of the project folder.
From your first screenshot, it looks like all of the corpus files and the jar are inside the src folder (you can confirm if this is the case if you collapse the src folder by clicking on the triangle icon). Use cut and paste them at the project level.

Issues with creating a JAR file with Eclipse

I have to extract a runnable jar file from an Eclipse project. I know that choosing Package required libraries into generated JAR makes the jar file slower than choosing Extract required libraries into generated JAR, but the problem is that if I extract the libraries, rather than package them, the application doesn't start because of the following error:
Catched FileNotFoundException: C:\Users\rober\Desktop\palbo-natives-windows-amd64.jar (The system cannot find the file specified), while TempJarCache.bootstrapNativeLib() of jar:file:/C:/Users/rober/Desktop/palbo-natives-windows-amd64.jar!/ (file:/C:/Users/rober/Desktop/ + palbo-natives-windows-amd64.jar)
Exception in thread "main" java.lang.UnsatisfiedLinkError: no gluegen-rt 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)
at com.jogamp.common.jvm.JNILibLoaderBase.loadLibraryInternal(JNILibLoaderBase.java:454)
at com.jogamp.common.jvm.JNILibLoaderBase.access$000(JNILibLoaderBase.java:59)
at com.jogamp.common.jvm.JNILibLoaderBase$DefaultAction.loadLibrary(JNILibLoaderBase.java:90)
at com.jogamp.common.jvm.JNILibLoaderBase.loadLibrary(JNILibLoaderBase.java:328)
at com.jogamp.common.os.DynamicLibraryBundle$GlueJNILibLoader.loadLibrary(DynamicLibraryBundle.java:390)
at com.jogamp.common.os.Platform$2.run(Platform.java:249)
at java.security.AccessController.doPrivileged(Native Method)
at com.jogamp.common.os.Platform.loadGlueGenRTImpl(Platform.java:231)
at com.jogamp.common.os.Platform.<clinit>(Platform.java:183)
at javax.media.opengl.GLProfile.<clinit>(GLProfile.java:99)
at org.jzy3d.global.Settings.<init>(Settings.java:12)
at org.jzy3d.global.Settings.getInstance(Settings.java:21)
at org.jzy3d.chart.Chart.<init>(Chart.java:59)
Of course, all the required libraries have been added to the Eclipse project and thus the application perfectly works if launched in Eclipse.
Is there a way to avoid this issue? In other words, is there a way to make my jar finding all the required libraries even if I choose the option Extract required libraries into generated JAR or Copy required libraries into a sub-folder next to the generated JAR?
Additional info:
even if I choose the option Package required libraries into generated JAR, I need to have some .dll files (e.g. gluegen-rt.dll, jogl.dll, jogl_awt.dll, jogl_cg.dll) in the same folder of the runnable jar files in order to make it opens correctly. Moreover, the application takes forever to be initialized.
Summary:
Extract required libraries into generated JAR or Copy required libraries into a sub-folder next to the generated JAR: the application cannot be opened because of the above posted Exception.
Package required libraries into generated JAR: noteworthy performance issue.

JAVA jar file to exe error

When I try to make my jar file into an exe file, when I run it I get an error. My java file while running in Eclipse works great.
Error occurred when starting the application:
I have used both exe4j and Launch4j.
exe4j gives this error:
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at EasyExpress.<init>(EasyExpress.java:61)
at EasyExpress.main(EasyExpress.java:162)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.exe4j.runtime.LauncherEngine.launch(Unknown Source)
at com.exe4j.runtime.WinLauncher.main(Unknown Source)
The immediate cause is most likely that you have used Class.getResource (or similar) to get the image from your application resource collection. When that fails, it returns a null. You have then passed that null to ImageIO.read. Hence, the exception you are seeing.
The problem is likely to be one of the following:
The resource is not in or available to the (ultimate) executable.
The resource is there, but your application is using the wrong path.
Here's what I'd do.
Generate an executable JAR for the application using Eclipse.
Run that JAR ... outside of Eclipse (!) ... to see if you get the same problem.
If yes, then solve the problem in the context of the executable JAR. It will be easier, and is highly likely that you will fix the problem for the generated exe file as well.
If no, then investigate how the exe4j tool handles resources and resource loading. (My brief read of the exe4j documentation did not highlight any obvious gotchas ...)
If you only want to run it outside eclipse then you could make a JAR file. In eclipse click File > Export JAR > Select Java JAR File > select project > select destination to save to. Done

Eclipse java lwjgl no lwjgl in java.library.path

I installed and putted lwjgl library and natives to /lib folder. On eclipse Java build path setted library location and native library location. When i try to run it on eclipse, it works, but when i export it as a runnable jar, it doesn't run, and throws error:
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:132)
at Main.start(Main.java:9)
at Main.main(Main.java:31)
Tried to set run properties VM argument to -Djava.library.path="lib/" but it doesn't work too.
P.S. Tried to put the native .dll's to same folder as the jar is and it works, but I want to .dll's to be in other folder.
Thanks!
Found an answer. You can do it with custom .bat file and put there -Djava.library.path="lib/", but mine solution is to programmaticaly do that with next line:
System.setProperty("org.lwjgl.librarypath",System.getProperty("user.dir") + "/lib/");
Have you tried using JarSplice? It is a free Java .jar packaging program found here:
Ninja Cave - JarSplice
Follow the instructions. All the documentation is on this site. Just make sure that the JAR you use for your program is just an eclipse JAR file, not a runnable jar. When you start up JarSplice, add the appropriate Jars, Natives, define your main class, and simply create a runnable JAR. No need to set up VM configurations, and also you can export to Windows.exe, and MacOSX. If all goes well, you should have a double-clickable runnable jar. Hope this helps. It's how I've packaged LWJGL games in the past :)

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