So I have a small Java program with some gui that I have runnning in eclipse just fine. I followed the fatjar tutorial to create the .jar, but when I try to run the .jar the first gui window appears but none of the functions work. When I ran it in cp, clicking the button generated an exception basically saying some of the object types could not be resolved as type, all of which were from import libraries. Has anyone seen this before, I tried using this program jarfix for an issue similar to mine, but nothing.
Please define
When I ran it in cp
The format of the java command should look something like
java -cp /path/to/jars com.main.class
You could start locating the source of the problem by opening the jar file with an archive program (e.g. winzip) and look if the class / type is included in the jar file or not.
If it is there it might be a classpath issue. If it is not there something is wrong with building the jar file. Musn't the included jar file be set in the "Order and Export" tab in the java buildpath dialog?
I am not aware of fatjar. But I faced similar problem, might be somewhere it is linked to your issue.
In my case everything was working when I was running through eclipse, but when I created JAR using Eclipse I faced issue as what you are facing, but in my case it was issue of accessing resources inside JAR File.
So solution was,
Right Click -> Export as -> Jar, here check the box "Add Directory Entries" and then create the JAR. Ans all worked.
Above all, first you should check whether the jar which is generate is correct or not by just open it with winrar or similar tool and see all classes and resources are placed properly.
An alternative would to place all those jar files in the same folder. i.e. if your application jar file is App.jar and other dependencies are A.jar and B.jar. Then drop A.jar and B.jar in the same folder as App.jar.
If you double click on App.jar it will by taking the other dependencies in the same folder by default.
Related
So my old jar has the libraries exported into it as folders containing the .class files. And my new jar has each library .jar packaged into my jar in a libs folder like my workspace. I need it to export with the former setup. Pictures below explain my issue further.
The reason the packaging changed is because I reinstalled my OS and I don't remember how I had done it before since it's been a while since I last exported a jar for this project, I think what I did last time to get around this issue was I used IntelliJ Idea to export it but I'd much prefer figure this out with Eclipse. There seems to be a setting that does exactly what I want under the Runnable Jar Export wizard but I'm not exporting a runnable jar as it doesn't have a Main method.
old jar picture:
new jar picture:
new jar picture 2:
Yes, a fat JAR can be created via the Runnable JAR file, but not via the JAR file export wizard.
So either
create a dummy main method or
use Ant, Maven, Gradle or something else
to create a fat JAR.
Make sure that you put everything in the source folder that you want to copy to the output folder and include in the JAR.
Switching to IntelliJ Idea allowed me to export in this manner out of the box with default settings. If anyone knows how to get this function in Eclipse feel free to add it.
Edit: as per #howlger's comments I've found a solution where Eclipse works perfectly for my use case.
I'm currently working on a project where I'm using the LWJGL library and I just tried building a .jar out of it all, now the problem is that nothing happens when I try to start the .jar.
My only thought is that maybe the library didn't export correctly but I've been looking around at the other questions here for a while and I've have added everything correctly at the Dependencies tab in the Project Structure, the libraries I need are also in the Artifact Output Layout as extracted directories.
I just tried checking the stacktrace with cmd and it says that it can't find lwjgl in the library path so I'm guessing there's something I don't know about .jar files or something that's causing this error.
Here's the stacktrace:
Also in IntelliJ I've put "-Djava.library.path=lib\native" in VM options, is it possible that this doesn't apply to the .jar or something.
Help is appreciated!
There is a rather complicated process to make an executable jar with LWJGL. You need to use Jarsplice.
Jarsplice has 4 steps which are pretty self explanatory but here they are anyway:
1: Add Jars.
Add the jar you generated as well as all the libraries. That includes lwjgl.jar, and any other jars you used.
2: Add Natives.
Add all the natives you used in your project. These are the files you referenced using -Djava.library.path.
3: Main Class
Select the main class of your program. This could be something like com.example.game.EntryPoint.
4: Create Fat Jar
Click "Create Fat Jar" to create your executable jar!
You can also optionally create a Windows .exe, OSX .app, and Linux .sh executables as well.
I have made use of JDBC in one of my projects and it worked flawlessly in Eclipse. I had to add the ojdbc6.jar to Java Build Path to the project BTW. (I have also not included any Class.forName statements in the code since I heard it's optional for JDBC 4 and above)
When I export the project into a jar file, and try to run it, it complains saying "No driver found for JDBC" and so on. I can see that this is because the ojdbc6.jar is missing from the build path now. Can someone please let me know of a solution?
Thanks!
Here is a simple method to export a JAR that uses external jar as libs:
You have to write a manifest file, I named it jar.manifest.
The content in manifest file looks like:
Manifest-Version:1.0
Main-Class:com.enginebai.activity.MyJDBCProgram
Class-Path:lib/ojdbc6.jar lib/some-externam-lib.jar
and make sure to put a blank line under Class-Path, otherwise the JAR won't run. (I have no idea why to put a blank line over there.)
Let's go back to eclipse:
In eclipse, you export your classes and remember to select above manifest file.
Make sure your ojdbc6.jar be in project/lib folder.
Open your terminal and cd to project root directory, enter `java -jar your-jar-name.jar', it should work then.
put ojdbc6.jar on classpath when you run your jar
I have done some research and found a solution that seems to work.
I have exported the project into a "Runnable Jar file" instead of just a "jar file".
This way, I was actually able to repack Jar files from my Build Path into the new Jar file I am exporting my project to. Simple and clean.
You also might want to look at signed Jar files and any other certificates in your existing jar files in the Build Path. They cannot directly be repacked through the export option.
I have a java project that works perfectly fine when running it from within Eclipse. When I try to export it to either a "JAR file" or "Runnable JAR file" the .jar file is created, but when I double click on it to try to run the program it gives me an error that says
"Could not find the main class: package.MainClassName. Program will exit."
As I mentioned, I tried exporting to both JAR options, I specified the correct class that the main method is in, and when I look through the actual files in the .jar file everything seems to be in order -- the manifest looks something like:
Manifest-Version: 1.0
Main-Class: package.MainClassName
(blank line)
and is in the META-INF folder. There is a folder with my package name, which contains all the .class files, including the class that contains the main method. A few image and text files that I use also appear in the jar file.
The actual program isn't anything too complicated -- it's a simple "snake" game using Swing (plus the code all works when run from inside Eclipse).
Any ideas what is causing this error and how I can fix it? Let me know if there's any other information I should provide.
Verify that you can start your application like that:
java -cp myjarfile.jar snake.Controller
I just read when I double click on it - this sounds like a configuration issue with your operating system. You're double-clicking the file on a windows explorer window? Try to run it from a console/terminal with the command
java -jar myjarfile.jar
Further Reading
Running JAR file on Windows
The manifest has to end with a new line. Please check your file, a missing new line will cause trouble.
Run it like this on the command line:
java -jar /path/to/your/jar/jarFile.jar
Ok, so I finally got it to work. If I use the JRE 6 instead of 7 everything works great. No idea why, but it works.
Had you tried creating a .jar file manually instead of using Eclipse. Try the following steps, hopefully that might help :
Considering that your directory structure looks like this :
TicTacToe(Your Project Name I mean)
| | |
src bin manifest.txt
| |
icons tictactoe
Now suppose that my main class is BeginGame inside package tictactoe, so I will write inside my manifest.txt file this thing :
Main-Class: tictactoe.BeginGame
Do remember the space between colons : and package name i.e. tictactoe, and immediately after BeginGame press Enter and save the file.
Now on your command prompt go to the location of bin folder, I am describing my side as follows :
C:\Mine\Eclipse\TicTacToe\bin>jar -cfm ..\tictactoe.jar ..\manifest.txt tictactoe icons
Here the first argument i.e. ..\tictactoe.jar is used to tell that create tictactoe.jar one level up, i.e. inside TicTacToe Folder.
Second argument ..\manifest.txt means that the manifest.txt file is located one level up, i.e. inside TicTacToe Folder.
Third and Fourth arguments tictactoe and icons means, that add both these folders to the .jar file, since they are placed inside bin Folder so they are used as it is. Now Press Enter.
Now try to run your .jar file so created inside the Project Folder (TicTacToe Folder, in my case).
Hopefully this will work.
Have you renamed your project/main class (e.g. through refactoring) ? If yes your Launch Configuration might be set up incorrectly (e.g. refering to the old main class or configuration). Even though the project name appears in the 'export runnable jar' dialog, a closer inspection might reveal an unmatched main class name.
Go to 'Properties->Run/Debug Settings' of your project and make sure your Launch Configuration (the same used to export runnable jar) is set to the right name of project AND your main class is set to name.space.of.your.project/YouMainClass.
I ran into the same issues the other day and it took me days to make it work.
The error message was "Could not find the main class", but I can run the executable jar exported from Eclipse in other Windows machines without any problem.
The solution was to install both x64 and x86 version of the same version of JRE. The path environment variable was pointed to the x64 version. No idea why, but it worked for me.
Right click on the project. Go to properties. Click on Run/Debug Settings. Now delete the run config of your main class that you are trying to run.
Now, when you hit run again, things would work just fine.
For netbeans user that having this problem is as simply:
1.Go to your Project and Right Click and Select Properties
2.Click Run and also click browser.
3.Select your frames you want to first appear.
you are just missing static keyword. that's all.
I've looked through many of the existing threads about this error, but still no luck. I'm not even trying to package a jar or use any third-party packaging tools. I'm simply running from within Eclipse (works great) and then trying to run the exact same app from the command line, in the same location it's built to (getting this error). My goal is to be able to zip up the bin folder and send it off to be run by someone else via a command line script. Some details:
It's a command-line app and I'm using the commons-lang-2.4.jar for string utilities. That is the file that cannot be located (specificaly "java.lang.NoClassDefFoundError: org/apache/commons/lang/StringEscapeUtils")
I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path"
The .classpath file looks correct and contains the reference to the jar, but I assume that file is only used by Eclipse (contains this line: <classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>)
Could this be related to the Eclipse working directory setting? I have some internal template files that I created that are under src/templates, and the only way I can seem to get those to be seen is by setting the project working directory to AppName/src. Maybe I should be putting those somewhere else?
Let me know if any additional info would help. Surely this is something simple, but I've wasted too much time on it at this point. This is reminding me why I originally left Java back in '05 or so...
A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.
In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.
The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:
java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
To save the effort of typing and editing the argument in commandline everytime, use a .bat file.
Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.
java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
Are you specifying the classpath to java on the command line?
$ java -cp lib/commons-lang-2.4.jar your.main.Class
The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.
If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:
java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar
If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).
Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with
java -jar yourProgram.jar
There are several variants depending on your needs.
Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See #BalusC's answer for good coverage of that.
Eclipse doesn't build executable java classes by default. Don't ask me why, but it probably has something to do with using their own tools.jar (somewhere in plugins/org.eclipse.core ?) so that Eclipse can run without a JDK.
You can usually go to your project bin directory and do:
java -cp . MyClass
But if you have external jars, Eclipse handles those internally in another weird way, so you'll need to add those too.
make sure your jar commons-lang-2.4.jar in classpath and not redudance.
I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth