I have received an IntelliJ project from someone else, but it won't run even the simplest classes for me.
As a quick example, the following class will throw a ClassNotFoundException without compiler errors:
package myPackage;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
System.out.println("Soon our GUI will start from here!");
System.exit(0);
}
}
This will throw the following error:
Exception in thread "main" java.lang.ClassNotFoundException: myPackage.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:260)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:116)
Process finished with exit code 1
The file structure is as follows:
<git root>/src/myPackage/Main.java
I have set the 'src' folder as the 'source folder'. My application has no compiler errors.
I have invalidated the caches several times, but with no effect whatsoever.
In my module settings my module dependencies contain <Module source> and jdk 1.8.
I can't seem to find the answer around here (google) anywhere, and to me it seems like this error has to do with the way IntelliJ handles the project. When I first pulled the project, the src folder had NOT been set to be the source folder, which probably means I may be missing out on other settings as well.
Zack Newsham's suggestion helped me. Here is what I did:
Deleted .iml file
File > New > Project, specified the existing folder with code
completed project setup (Manifest, artifacts).
Now the project runs.
Of course there may be other reasons for ClassNotFound. However when everything seems ok or if the error comes up after it worked before, try these simple steps.
Related
I'm using IntelliJ IDE on an armx64 linux-based system, where i'm working on a non-maven java project that have a lot of modules (dependencies) linked to it. When running my project from the IDE everything works fine, however when building it into a runnable jar file and trying to run the jar from the terminal, using java -jar myjar.jar, i got the following error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:330)
at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:263)
at java.util.jar.JarVerifier.processEntry(JarVerifier.java:318)
at java.util.jar.JarVerifier.update(JarVerifier.java:230)
at java.util.jar.JarFile.initializeVerifier(JarFile.java:383)
at java.util.jar.JarFile.getInputStream(JarFile.java:450)
at sun.misc.URLClassPath$JarLoader$2.getInputStream(URLClassPath.java:977)
at sun.misc.Resource.cachedInputStream(Resource.java:77)
at sun.misc.Resource.getByteBuffer(Resource.java:160)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:454)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
I'm currently using OpenJdk-1.8, i tried to use Oracle JDK v1.8 instead to build and then rerun the jar file, but still facing the same error. I'd also tried to create a new simple "Hello world" application and export a corresponding jar for it where it runs successfully without errors.
Here is my jar internal structure and the corresponding manifist file:
jar structure screenshot
Thanks for #y.bedrov who linked to this issue that helped my solving the error, where it includes the following:
"sqljdbc4.jar" was the signed JAR in OP's external libraries. So,
following above approach to systematically exclude the signature
related files like .SF, .RSA or .DES or other algorithms files is the
right way to move forward.
And yes I'm also using "sqljdbc4.jar", so after reading the answer i decided to solve the problem by deleting the signature files from my Meta-inf folder inside my jar file.
I've looked at many of the other similar questions posted but it seems that a lot of it is about exporting and using a .Jar file.
I'm trying to only compile my code. I had it on github but when I cloned and tried to compile the same program on a new computer, I get the following error:
java.lang.NoClassDefFoundError: com/intellij/uiDesigner/core/Spacer
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.intellij.uiDesigner.core.Spacer
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main"
Process finished with exit code 1
I am currently using jdk 1.8, if anyone can help me compile my program it'll be great, here's a snippet of the main class
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("CertifyGUI");
frame.setContentPane(new CertifyGUI().panelMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
This code uses classes from IntelliJ IDEA GUI Designer, for this code to compile and run you need to include these classes in the classpath and build the code from IntelliJ IDEA or using javac2 Ant task.
The missing classes are available in IDEA_HOME\redist\forms_rt.jar.
When building from IntelliJ IDEA, the required classes are copied to the output directory (classpath) automatically and the .class files are instrumented with the GUI initialization code based on the .form files.
There are ways to build it with Maven/Gradle/Ant in the command line, you can Google the relevant answers if that is what you need (javac2 maven, javac2 gradle, etc).
This error
NoClassDefFoundError: org/testng/TestNG
appears when I'm trying to run my test from Testng.xml file using IntelliJ IDEA.
Running my test one by one works perfectly as well as running whole gradle project which points to testng.xml, like grade core-test:test
( On my project I'm using Appium + IntelliJ + TestNG )
But when I'm running testng.xml using IntelliJ I'm immediately getting this message after pressing Run:
Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 18 more
I found a solution posted here: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206597869-Cannot-launch-NGTest-runner-in-IntelliJ-IDEA-2016-1
I got it to work by selecting "In whole project" (I had "In single
module" selected) under the Configuration tab in the TestNG "Run/Debug
Configurations."
However, the prior configuration worked in IntelliJ IDEA 15, so to me,
it seems that it may have come from a breaking change with newer IDE.
It worked for me.
adding a screenshot to aid Justin's answer above. The default is In single module which needs to be changed to In whole project.
For me it is a little different, i was using testng configuration file to run a testng suite. I was running it from the parent module instead of the child module. The following is what I did to overcome the error
Follow the steps -
1. Add the TestNG library from java build path - go to project --> right-click--> properties--> java build path --> third option libraries.
TestNG - sELECT and then apply and close
PLease follow following steps:
Add the TestNG library from in java build path for the project.
Right click on the project then -> Build path -> Configure build path -> Libraries tab -> Add library -> Choose TestNG and apply.
Create a testNG class and run the testng.xml file, it will work.
Right click on project -> testNG-> create testNG class -> give the details and create one method in that class then run the file.
for me, this was a little different still. I just started using IntelliJ in 2019, and it worked fine until around Christmas. Here is what finally fixed my misbehaving system that couldn't run IntelliJ (because i could run the same files from the same repository on another system running the same IntelliJ version)
Had to change the template TestNG to use a Test Kind of Suite instead of Class, then set this to whole project.
showing where i changed the project to Whole Project
Tried all the answers mentioned in this post, in addition to all this I had to remove Build from the section Before launch: Activate tool window to make it work:
Screen Shot
Mine was a little different, using Allure to generate reports with TestNG. In the Allure section of the gradle.build file, I needed to upgrade from 2.8.1 -> 2.13.8. For some reason 2.8.1 was causing the class not found exception for TestNG, some logging improvement needs to be made on their part.
def allureVersion = "2.13.8"
allure {
autoconfigure = true
aspectjweaver = true
version = allureVersion
clean = true
useTestNG {
version = allureVersion
}
}
Exclaimer: I am pretty much a complete noob when it comes to eclipse and java, but I am learning! :D Thanks for your help
I am using the apache commons io to create an external server log that is saved in a file along with showing in the server console itself. I added the right file to the external jar library and it all works inside eclipse, but when I export it and try to run it on it's own, I get these errors
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/output/TeeOutputStream
at com.josh.chat.server.ServerMain.<init>(ServerMain.java:10)
at com.josh.chat.server.ServerMain.main(ServerMain.java:17)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.output.TeeOutputStream
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
What is going on? How do I fix this, or at least is there another way for me to use the console AND save a log of that console to a text file?
EDIT: I didn't change anything (at least I don't think I did...) and tried to run it again and then got this different error, what did I do?
no main manifest attribute, in InstantChatServer.jar
Probably your issue might have been fixed by now but on broader scope if you need further dependencies/libraries to be there then you will have to edit the class path and also the client(the one who is doing the java -jar ... stuff) needs to have those dependencies be present in his box.What if there are hundred such dependencies. Ideally you should prefer a build tool like maven,gradle etc. and bundle the jars/dependencies with your code. So that the client need not have to bother about having the libraries in local box.
I'm developing a eclipse plugin rcp and I'm running into a NoClassDefFoundError
Exception in thread "Thread-7" java.lang.NoClassDefFoundError: org/jdom/input/SAXBuilder
at org.geonames.WebService.search(WebService.java:783)
at geo.GeocoderGeonames$SearchThread.run(GeocoderGeonames.java:119)
Caused by: java.lang.ClassNotFoundException: org.jdom.input.SAXBuilder
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClassInternal(BundleLoader.java:483)
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:399)
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:387)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:87)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 2 more
The class that supposedly cannot be found is in a jar that I have added to the buildpath. I don't get any compile error etc only this exception when the running application enters the code where this class is needed...
Is there some other place I need to add the jar
After reading this added the jar to the MANIFEST.MF, which solved the problem.
As I understand it, eclipse starts several classloaders which only sees what MANIFEST.MF tells them to see and ingnores the build path...
How are you running your plug-in? You may need to add the JAR to the class path in the VM arguments.
In our experience a NoClassDefFoundError can sometimes mean that more than one version of a Class are found, as there is also a ClassNotFoundException that's normally thrown if a class cannot be found.
Another reason in your case (XML parser) might be something with endorsed classes. Are you directly importing the jdom classes or something like org.w3c...? If so, have a look at the "endorsed classes" system of Java, something that I just recently came across.