NoClassDefFoundError in simple .jar file - java

I'm trying to create a simple .jar file out of my project. The project is made of two .class files - the main class which uses the secondary class to generate a GUI. The main class is the actual "main" class that runs while the second class is just a class file with it's methods and it's also an extension of JFrame and imports javax.swing and java.awt.event.*.
I use Jar to bundle it all up. I add a manifest file (with a new line character) which points to the primary file with the main method. The Jar file thus has two .class files and a folder with the manifest.txt in it. When I use javaw.exe to run it, nothing happens at all. So I try to run it in the command line and I get a NoClassDefFroundError about the secondary class.
I noticed I get the same kind of error when I try to compile and run the second class in JCreator - no wonder, it doesn't have a main method, it's just a class file. When I run the main file from JCreator, everything works fine.
Any ideas?

Looking at your stack trace, I can now see the problem: I can tell you actually have more than two classes:
Caused by: java.lang.ClassNotFoundException: grafPrime$calcButton at
There's a file named grafPrime$calcButton.class, and it needs to be in the jar file, too. There may be other such files -- make sure you include all of them!

Okay, the problem is that you've not included the anonymous class - you should have a file called grafPrime$calcButton.class, and that's not in your jar file.
Basically, compile your code into a clean directory and include all the class files which are generated.

Related

How does an executable jar file work with another program

I want to know how does an executable jar file (which is added as a library) work with the main program.
For example when an executable jar file is added, and if it was not called within the source code of the main program, does it have any effect?
Is an executable jar file the same as a run time library?
An "executable jar file" is simply one that has a class that defines a main method and where the jar file maintains metadata about where to find this main method.
If this jar is not the jar used to launch the JVM, then this metadata is ignored and the jar is treated just like any other jar file (no effect unless explicitly used by something else).
Answer is No, It is just standard JAR file which contains a class with Main method init. This main method's location information gets stored in metadata.
If you run this jar file then this class & thus main method gets called.
Otherwise it is just normal JAR file. You can add it to any application's classpath & it has same effects as other normal JAR files. Main method still exist but won't get executed.

Java NoSuchMethodError same class name

I have two .class file with the same name and same package in two different .jar file
First jar:
Second jar:
When i run the program from eclipse i haven't problem, eclipse use the first .class file (i must use the first . class, i don't need the second .class, i want exclude it).
When i export runnable .jar i saw that is executed the second .class file and then i have
NoSuchMethodError exception, because the second .class is different from the first.
How can i use always the first .class and exclude the second?
I don't need the second .class, but i need other class from his library.
Java loads classes from classpath that is defined dynamically when you are running application in eclipse and is controlled by property Class-Path in file MANIFEST.MF located under META-INF in you jar file.
So, first open jar file using any ZIP tool and take a look on manifest. Try to change the order of jar files into manifest and run again. I hope this will help.
BUT this is extremely bad that your alive-matchmarker.jar contains file that it should not contain. I do not know what library is it but is there a chance that they have other distribution that does not contain their own dependencies? Or probably try to find other version of this library. The worse thing that can be is if you have different versions of the same class in your classpath: the behavior of your application can be buggy and unpredictable as a result of this duplication because you can never know what version of class is used now.
Do not import the whole package like
import org.mindswap.*;
You can import specific class you want from any specific package like
import org.mindswap.wsdl.WSDLTranslator;

Doesn't find my main class in a jar

I'm developing a program with three classes and also includes some external jar archives and classes. When I run it in Eclipse it works properly, but I need to try with some other programs, so I need to run it at the console. I save all of it in a folder, which contains another two folders, one with the .class created by me and the other one with the .java and .jar archives and a folder with the external classes. I've tried to creat a .jar archive containing this folder and the manifest, where I told where's the main class.
When I run it I receive "Exception in thread "main" java.lang.NoClassDefFoundError", so it doesn't found the class where the main is, but I don't know why. I've tried some different ways to define it at the manifest and changing the classpath and it still doesn't work.
Any solutions or advices?
Thanks!
Try to use JarSplice, it will let you define the main class.
NoClassDefFoundError means that the file existed during compile time, but it was not found during run time. Since there were no compilation issues, the jar was created, but at run time, while using command like java -jar jarfilename fullyqualifiedclassname it threw this exception. Check the classpath variable, validate that you are executing the command from the right folder location in command window.
I had always referred the below link for such errors and it worked for me
http://javaeesupportpatterns.blogspot.in/2012/06/javalangnoclassdeffounderror-how-to.html
when creating a project for example ABC the IDE normally Netbeans creates the source folder and creates the main class as ABC.java which contains the main class. In Netbeans IDE let you mention your main class if it is not ABC or if not you must create an instance of the class you want to run when application executes in ABC.java class.
In one of my projects I ran also in the same situation. The problem was always a missunderstanding of mine with this error message.
The reason in my case was that the computer could not found the declared jar file. The use of the parameter -cp let me became happy. So try
java -jar -cp ./Server.jar
Edit:
I forgot to mention that I had also to mention the full qulified name of class which contains the main class.
This:
The stacktrace is "Exception in thread "main" java.lang.NoClassDefFoundError: /bin/TextClient
looks to me like your Manifest is wrong. To correct it, you can use:
jar -uvfe Server.jar full.name.of.your.MainClass
Note: not the file/path name of some class file!
This will create a MANIFEST that tells that full.name.of.your.MainClass is your main class.
In addition, if your jar contains entries like:
bin/Foo.class
bin/...
you created the JAR file probably from the wrong place (unless your TLD is bin).
If your main class is named org.adepts.Main, then a top level entry should be:
org/adepts/Main.class
(This is where it will be searched.)
I finally solved it by using the Eclipse "Export" option and not creating it manually.

eclipse for Java NoClassDefFound error on main entry point - package, project and class are the same

Trying to create a windows executable but always get error on Exception in thread main java.lang.NoClassDefFoundError. I've read all the other responses but so far my issue remains the same.
I have a class file called testproject that has a main procedure that is public static void. My class file also have a package designator at the top of the file called testproject. My class file compiles successfully into a file called testproject.class.
The command below works but when I run testproject.jar, I get the above error:
jar cvfm testproject.jar c:\temp\manifest.txt *.class
Contents of manifest.txt:
Main-Class: testproject.testproject
I've tried many combinations of Main-Class
please add the code that you have written in your java file.Otherwise it can not be tracked.
seems like the jvm is not able to find the class file for the Main class.the possible root causes could be
The files are not generate at correct places ,Try extracting the jar file an see if the classes are there in correct package folders
The Manifest file or the jvm command line classpath or the manifest file doesn't contain the entry for path to the class file
Try using IDE for generating the JAR file , that usually helps
It seems like you are executing the command "jar" from your package "testproject":
jar -cvfm testproject.jar c:\temp\manifest.txt *.class
Try to execute it from parent folder:
jar -cvfm testproject.jar c:\temp\manifest.txt testproject/*.class
The class file will be put into the "testproject" package.
By the way, be ensure that your manifest file has a new empty line at the end.

How do I use manifest.mf to contain correct location of the main class?

Can anyone help me with this? I have not been able to find anything that answers this for exactly what I need. All the answers I find have to do with adding additional libraries to manifest files and what not.
Here's the situation:
I have written a game using NetBeans 6.9. The game is in Java. There are about 80 classes. All classes are contained in the default package. The game executes correctly. I've been working on this project for about 18 months and I always maintain executable code as I have developed the game.
What I did:
I needed to import something from a static class I'd written, into another class I was working on. In order to do that, I had to move everything out of the default package. I used NetBeans to refactor everything into a custom package.
What happened:
When I do a "clean and build", the project builds successfully.
When I do a "run main project", I get the following error:
java.lang.NoClassDefFoundError: WarMachine
Caused by: java.lang.ClassNotFoundException: WarMachine
...stack trace
Could not find the main class: WarMachine. Program will exit.
I have checked the .jar file and I see that all the compiled .class files are there, including the main class (called WarMachine.class). All the .class files are in a directory called Machine (that was the package name I had NetBeans refactor everything into).
From what I have been able to find on the internet, the problem is that my manifest.mf file does not point to the correct location of the WarMachine.class file.
What does my manifest file need to say? I don't use any other libraries or anything like that. I have 78 classes, all of which are in the "Machine" package. Can anyone help me?
Thanks for your time!
You have to tell Netbeans where the main class file is. Right click on your project, select "Properties", then go to the "Run" tree element on the left.
You will now have a "Main Class" textbox on the right. Click "Browse" and select your main class. Netbeans should then fix the manifest file for you.
In case you are curious, your manifest file inside the .jar file of the dist folder should have a line like this:
Main-Class: Machine.WarMachine
As an aside, it is considered standard practice to use lowercase letters for package names. You should call your package machine.

Categories