Unable to run JAR file (created in Eclipse) - java

I hate starting a post with this but I'm new to Java... I've followed this tutorial to create a socket server (mines in Eclipse). I can run the server within Eclipse, all is well. But when I try to export the project I can't figure out how to run it. I keep getting this error (it varies depending on how I run it)
Exception in thread "main" java.lang.NoClassDefFoundError: xsocketserver/Main
Caused by: java.lang.ClassNotFoundException: xsocketserver.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
I've read this problem relates to class paths being set. I've tried the following:
java -cp libs/xSocket-2.8.12.jar xsocketserver.Main
java -jar xSocketServer.jar
java -classpath xSocketServer:xSocketServer/libs/xSocket-2.8.12.jar xsocketserver.Main
plus many others.
The file structure within the JAR is as follows:
xSocketServer.jar
-> xsocketserver
-> Main.class
-> xSocketDataHandler.class
-> META-INF
-> MANIFEST.MF
-> libs
-> xSocket-2.8.12.jar
Incidentally I've tried adding my own manifest file which contains the Class-Path but when I check it it always reads:
Manifest-Version: 1.0
Main-Class: xsocketserver.Main
I'm guessing this is a common problem based on the number of hits I've seen in Google but I can't fathom what I'm doing wrong. Wrong Export settings maybe??

I don't believe a jar-file can include another jar (as you've included xSocket-2.8.12.jar). If xSocket is your own code, let it be included directly in the "outer" jar instead.
Check out
http://www.velocityreviews.com/forums/t143595-jars-containing-jars.html
and
Classpath including JAR within a JAR
Otherwise it looks right to me. Make sure you're not trying to run an old/stale version of the jar. (Delete the jar and make sure eclipse exports a new one.)
How are you exporting the .jar file from eclipse? The generated jar-file will usually be based on one of your run-configurations. Make sure you use the one that you use when it works from eclipse.
Unless you've already tried it, try following the steps (listed at the site below)
http://forums.sun.com/thread.jspa?threadID=5358121
1-Right click on your project and select export option.A export window will get pop-up.
2-Select jar file option from the poped-up window which will be in java option of window.
3-After Clicking JarFile option a new window will get pop-up.
4-Select the export destination for ur jar file and click next.
5-After clicking the next a new screen you will see, click on next button again.
6-Now u will see a new screen which has a field name as "Main Class" browse for your main class of appliaction.main class is once which has main method.
7-Now select finish.

I had a similar problem and yours may or may not be related...
The Jar manifest file has to be in UTF-8 format. I had used windows notepad and that did not work. When I recreated the manifest file with another editor, it worked fine.
I assume eclipse will create UTF-8 files but I'm not sure if it will automatically convert if they are not in that format
If you did use another editor to create the first version of the manifest, try deleting the manifest.mf and recreating inside of eclipse.
John

Related

Running .jar File Java 8

I've run .jar files before, but I've encountered a "different" situation, and I'm not sure what to do. I'd appreciate if someone could help me out.
Previously, I programmed with Java 6 and Eclipse Juno exported all my programs to runnable jar files. I'd get a .jar file that I could run by just double clicking on it. The files always looked something like this (note the jar file icon):
Recently, I wrote a program in Java 8 with Eclipse Luna (Release 4.4.0) and exported it to a runnable jar file, and I got something different (note the different file icon):
It no longer runs when I double click it. Instead, my computer uncompresses the jar, as it would a zip file. I tried running it from terminal. I cd'd to the directory and typed
java -jar graph3D.jar
I got the following error message:
Error: Unable to access jarfile graph3D.jar
After uncompressing the jar file, I found a folder named META-INF with the manifest file, MANIFEST.MF in it. It was the only file that seemed to resemble an executable file. Do I have to do something with that?
Could someone explain how I can run the second jar file graph3D.jar? Is it something new with Java 8, or something different about Eclipse Luna, or something else?
(Both programs run fine in Eclipse, by the way)
Thanks for your time and help.
Edit:
Below was the dialog box Eclipse displayed if anyone is interested.
Selecting "Use .jar;.zip" makes the filename "graph3D.jar;.jar;*.zip" .
Selecting "Use .zip" makes the filename "graph3D.jar;*.zip"
Selecting "Cancel" doesn't let you go forward.
You'd have to manually delete the extra file extension.
Somehow when you exported the file, the filters for the file dialog box (*.jar;*.zip) got attached to the filename, which is graph3D.jar;*.jar;*.zip, not graph3D.jar. Java can't find it because it doesn't have the name you supplied. Rename the file and pay close attention next time you export; either you fat-fingered something, or you're triggering a significant bug that needs fixing.
I recommend that you will access the build folder after you've built your project on the IDE under your project folder (in your workspace) and copy both the libraries folder and the .jar and post them wherever you want the program to be "installed", you'll then have an executable jar that should run smoothly without problems, just as I said don't forget the lib folder.
I think there is nothing new in Java 8 related with the running jar, I guess you need to check the the Eclipse export issues, it seems your classes are missing from your second jar file.

Java application will run from CMD and Eclipse but not double click

I've done a search and I can see that a lot of people have had the same problem as me, but none of the solutions have worked for me.
Basically I have a Java Project in Eclipse that is from my old Windows Installation. I've cleaned and rebuilt it because at first it wouldn't compile, but now I have it exported as a Runnable Jar. However, the only way I can get the application to appear is to do java -jar foo.jar in command prompt, or run it in Eclipse. If I double click the JAR in Windows Explorer nothing happens even though I know that Java is associated correctly because other Runnable Jars work.
The project only has the x86 JRE listed in it's Build Path Libraries and all the files listed appear to exist. I'm running Windows 7 HP.
Update: I'm sorry, but I just discovered that no other Runnable Jars are working either. If they are wrapped with launch4j they work though...
Edit: The Runnable Jars that I export from Eclipse do work fine on other systems and load on double click
Some registry values or file associations are probably messed up. Wiping off all of your existing JRE's and JDK's and re-installing them should fix your issue.
Alternatively you may be able to fix it by manually editing the registry value here:
HKLM > SOFTWARE > Classes > jarfile > shell > open > command
My value is
Type: REG_SZ
Data: "C:\Program Files\Java\jre8\bin\javaw.exe" -jar "%1" %*
You'd of course want that path to point to your javaw.exe, and make sure you have the additional arguments.
I was also facing the same problem while i was working with Spring tool suite.
You may use the following steps:-
Right click on project -> export -> Runnable jar file -> (Here,In library handling,there are three options,you have to choose middle one i.e package required library into generated jar.It will package external dependency also).
-In my case, my runnable jar was only executing on my environment i.e on which i have created that JAR. Initially i have selected the first option to create JAR i.e extract required libraries into required JAR.but that was not proper.
It may help you.Let me correct if i am getting wrong.
Since you are able to run the JAR running the command line, I believe your issue is related to which version of Java is set to run the file when it is double-clicked.
To find out which version is successfully running the file from the command line and set it to open JAR files by default:
Open a new command prompt window.
Run echo %path%. Among the path values, you should be able to find one pointing to the bin folder of one of the installed versions of Java. Copy this path somewhere.
Navigate to the JAR file you would like to run. Right click the JAR -> Open with -> Choose default program... -> Browse...
Browse to the path you copied in step 2. (the easiest way is to paste it into the address bar)
Double click javaw.exe.
Click OK.
You should now be able to run the JAR file. Please let me know if your problem persists.
I have made a jar with and keep it on desktop.Then,I double clicked on the jar and it working fine for me.
How I and what I have monintored: In my main class, perform some operation and at the end I add on Thread.sleep(25000); to hold the program for few moments. After every double click on the exported jar I found one new javaw.exe process added in the system process tree. I have noticed it on Task manager. and after 25000ms respective javaw.exe process ended. As my application does not cointain any GUI that's why I have not seen any GUI changes for the respective process. I'm Confident that if my application have some GUI, I will surely get the respective GUI window on every run.
Common mistakes : when we export a project from Eclipse as Runnable JAR file, it is exported with selected Eclipse launch configuration and Eclipse specific launch wrappers. Now if the configuration does not match when you are trying to run it via double-click you will not be able to see the error, If you run it from CMD then surely you will get the error log.
To create standard executable JAR file : To create a standard executable JAR file, you can export as JAR file and specify the main class in last screen of the wizard.
That's working for me fine. I used the same jar from different system and keeping it different location.

"Could not find main method from given launch configuration" when using Java+Scala+Slick2D

I've got a project in which I'm using Java+Scala+Slick2D.
The project itself runs well when launched from within eclipse. But when I try to make a jar file, it just refuses to work. Here's the error I keep getting when trying to export it as a Runnable jar:
And if I try to export into just a Jar file, it's unable to find the Main Class:
There is, of course, a main class in game.TicTacGame. But it refuses to acknowledge it. I tried creating an executable with a simple Hello World project and it worked fine. It even detects the main class. Why is Eclipse not detecting the main class in this case?
PS: I've also tried extracting the .jar file created, editing the manifest.mf file to add the Main-Class: game.TicTacGame, enter two new lines and recreate the jar. Then it gives me a corrupted jar file error.
I'm at my wits end and would appreciate any help in this regard. I'm using Windows 7 x64 with Eclipse Juno, Java 1.7 and Scala 2.10
Edit: The Main class is in Java
Okay, I got it to work. Apparently, all I needed to do was restart eclipse. And then magically, it started detecting the Main class:
But the jar started giving me noClassDefFound errors for the Slick2d, LWJGL and other libraries. That's where JarSplice came to my rescue. I exported the project as before, with all the libraries and resources.
Then, I fired up JarSplice and added everything as follows:
Go to "Add Jars" and add the project.jar just created through eclipse, add lwjgl.jar, slick.jar and scala-library.jar. lwjgl and slick should be in your project lib folder where you would have imported them. scala-library should be available wherever it says it is. Make sure you add that as well
Next, when I tried to "Add Natives" it kept giving me "Duplicate library" error. So I removed them all and kept it empty.
Next in "Main Class", I entered the path to the main class i.e. game.TicTacGame
Finally, "Create Fat Jar". And it works perfectly :)
I just encountered the same problem, and here is how I solved it:
Open "Run As" --> "Run Configuration" on the project you want to export
Click "Search" for Eclipse to refresh the list of main class
Then export Runnable JAR file again, and everything goes smoothly.
The most easiest method is run the java file once and automatically the file appears in the list.Even i was facing the error but it was solved by using this simple method.
I recently figured a better way to do this using 'Runnable jar export' which might help you. In order for your main method to be listed in that list, you need to add the main method to the Run Configuration list.
This way it's simpler to create a runnable jar especially if you want to do it repeatedly.
Well, got the same Problem and solved it by selecting my GUI to export and not the whole Project.
I had this problem with Eclipse version 2019-03 (4.11.0) and compiler JaveSE-11. Choosing the right launch configuration and exporting runnable jar failed over and over again with an error
Could not find main method from given launch configuration
I've tried to restart Eclipse and do a clean build, but it didn't help.
In the end, I found a workaround to go into the generated jar file (I've used 7zip) and change META-INF/MANIFEST.MF file. The file should have something like this inside:
Manifest-Version: 1.0
Class-Path: .
Main-Class: <package name>.<class name>
Hope it helps someone.
I faced the same issue and in my case I found that "launch configuration" was incorrect in Runnable Jar file specification dialog. Somehow, eclipse was automatically taking it. The "Launch configuration" should be java file which has main method. Basically, it is filename-package.
The moment I changed my "launch configuration", I was able to create Jar file without any error.
I had the same issue. To overcome this issue, you need to close all opened files and open a single java class file that has the main() method. Then export it from eclipse and works fine.

.Jar file, created by netbeans doesn't open anymore on windows

For some reason, my application stopped working when I used "clean and build" at NetBeans and try to run it from dist folder. Application used to open from the jar file, but now it only blinks, and even doesn't give any error messages. Application runs, if I test run it with F6 using NetBeans. Jar file is created by NetBeans, so I guess the manifest should be okay.
Here's the link for the jar file...
Executing the jar in the terminal gives this exception trace:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:155)
at tbs.ImageLoader.loadImage(ImageLoader.java:11)
at tbs.Flag.<init>(Flag.java:21)
at tbs.Model.<init>(Model.java:58)
at tbs.GameView.<init>(GameView.java:33)
at tbs.GUI.<init>(GUI.java:36)
at tbs.Main.main(Main.java:6)
So it looks like you had something like this here:
public Image loadImage(String name) {
return new ImageIcon(getClass().getResource(name));
}
... and the getResource() method returned null, which caused the ImageIcon constructor to throw the Exception.
In line 21 of Flag.java you used "images/flagNeutral.png" as the image string, but your jar file contains images/flagneutral.png (inside the tbs directory). See the difference?
If it worked on your local system outside of the jar, you are using a case-insensitive file system there. (Windows or Mac?)
In the jar, as well as over HTTP and on "real" file systems, the URLs are case sensitive, which means you have to name the resource precisely as the file is named.
And yeah, normally you should have at least tried your program yourself, and posted the stack trace as well as the relevant code lines.
There may be a lib folder in the dist directory. If so, it contains jar files for any libraries you included. Make sure that is the case. You need to distribute the jar as well as the entire lib folder and store both in the same folder just like Netbeans creates them.
I am hoping you have created a Java application project and not a Java Class library project.
You can check if the main-class attribute and any library paths are added properly when you "clean and build" the project.
You can run it like java -jar tbs.jar and see the response.
Typical error when you don't define the main class before clean and run.
Click right on the project -> properties ->run section ->define the main class.

"Could not find main method from given launch configuration" error when exporting jar from Eclipse

I developed my program using Eclipse as my IDE. I used to use File|Export| Runnable JAR file option for creating dist build for client.
I was working with project using Run or (often) Debug button in eclipse (so I have configuration for project with valid main method).
Unfortunately when I created build yesterday I had following error when I tried to run app:
Failed to load Main-Class manifest attribute from My.jar
Then when I retried exporting I found the following problem in export log:
Could not find main method from given launch configuration
I have no idea what could cause this problem. I think I hadn't change anything in my project configuration. And I'm test my builds so I'm sure this option used to work correctly.
I will appreciate any help.
Are you selecting correct 'Launch Configuration' in the export dialog? Make sure it's from your project.
Also, once exported you could unzip jar file and look into 'META-INF/MANIFEST.MF' file - there should be 'Main-Class' entry with fully qualified name of your class where the main() method is.
I had this problem and none of the other options worked. Even removing and starting with a new workspace. In my case the problem was that I hadn't manually added the "classes" directory of my project to the classpath of the debug configuration that I was using to debug the java app. I don't know why it didn't pick up the classes directory automatically as part of the project from which I was running the app. In any case it started working again after that.
just had the same problem :s
it's annoying but easy to manually fix in the manifest file.
open the .jar file with winrar or 7zip.
locate the manifest file (META-INF folder)
change it to this.
Manifest-Version: 1.0
main-class: (package).(main-class)
In my case i had it in the default package.
Manifest-Version: 1.0
main-class: run
If you are not able to find your class file in Launch configuration, Right click on your project - > run as Java Application. Now export again.

Categories