Java IntelliJ Exception in Thread "main" java.lang.ClassNotFoundException: - java

So I've been getting this error pretty much on any new installation of IntelliJ on multiple computers.
The code I'm running is a simple hello world system out class.
public class hello {
public static void main(String[] args) {
System.out.printf("HELLO WORLD");
}
}
That's it.
Everytime I run this or anything else I get the error:
Exception in thread "main" java.lang.ClassNotFoundException: hello
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)
I've tried:
Revalidating my cache files
recompiling the project
setting the environmental variables to find the jdk and jre installations
setting the correct source folders
reinstalling jdk on windows and installing on ubuntu
at one stage I had managed to get it running when i used a default java class template on something, it had a import or package statement, but I can't remember what it was, however after adding the import/package statement things had worked.
UPDATE: the package that appears to get it working is "package com.company;", why does this work? what does it do?
Any help would be greatly appreciated.
Thanks Guys.

Related

Jar can not resolve org.apache.commons import

I am not particularly well experienced with Java at all and try to get a jar file running on my Ubuntu machine (https://sites.google.com/site/communitydetectionslpa/home).
However once I run the jar file with the command suggested by developers I receive the following error:
java -jar GANXiSw.jar -i test.ipairs
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/map/MultiKeyMap
at Net.<init>(Net.java:38)
at SLPAw.<init>(SLPAw.java:146)
at SLPAw.main(SLPAw.java:2050)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.map.MultiKeyMap
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
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)
... 3 more
Apparently java is not able to import the org.apache.commons class properly. After some researching I checked if I have libcommons-collections3-java installed, which however is the case.
I read something about adding the library explicitly to my CLASSPATH, which however I also read to be not good practice.
What is the best approach to fix my issue?

LibGDX and IntelliJ 15?

I installed LibGDX and IntelliJ15.
Then I built my LibGDX App and followed the instructions in this guide :
https://github.com/libgdx/libgdx/wiki/Gradle-and-Intellij-IDEA
I imported the programm with gradle into IntelliJ.
Whenever I try to start the DesktopLauncher, I get the following errors: Exception in thread "main" java.lang.ClassNotFoundException: com.hit.game.desktop.DesktopLauncher
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
I also tried to shorten the DesktopLauncher to:
System.out.println("asdfasdasd");
//LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
//new LwjglApplication(new HitGame(), config);
But I still get those errors :/
Does anybody have an idea or a hint what I am doing wrong ?
Wow,
after spending now more like 2 hours on this problem, it is a specific BUG in intellij 15.0.x ...
https://youtrack.jetbrains.com/issueMobile/IDEA-147788
One way to "build" properly is described in a Reddit post of User anubiann00b :
1. Run gradlew cleanIdea and gradlew idea, specifically in the InteilliJ terminal.
2. A popup comes up asking you to reload the project. Hit accept.
3. Ignore the message to import an unlinked gradle project.
4. Error message on make tells you there isn't an Android SDK. Go to the android module and change the SDK from Java to Android.
https://www.reddit.com/r/libgdx/comments/3tgjqi/using_libgdx_with_intellij_idea_desktop_module/
I've solved it by clicking on the desktop project then run>edit configurations>click on +>gradle>set Gradle project to Project:desktop and type run in tasks and apply and run

Exception in thread "main" java.lang.ClassNotFoundException:

I recently installed intellij IDEA 14.0 and to make sure everything was working I created a simple Hello World program. I do not understand why the output is not correct and why I am getting this error. If someone could please help that would be great.
This is the program:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
This is the error:
Exception in thread "main" java.lang.ClassNotFoundException: Hello
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)
The reason you're getting this error is because you didn't save the class into a file called Hello.java (case-sensitive!)
This is just problem of intellij IDE.
Solution Steps:
CtrlShiftAlts -> Preject Setting -> modules tab -> expand <project_name> and go to <project_name>_main
refer Image:
In Sources tab -> ** click on x symbol**
click YES
Right click on -> sources
Apply and OK
Finally build your project.
Enjoy.
It usually happens with IntelliJ.
I solved it deleting all modules and only leaving which have more hierarchy
The other modules(like main or test) are already part of product

Why won't this project run NetBeans, but it does in Eclipse

I have built a small program for school that runs perfectly when i run it through eclipse, but if I run the same code in NetBeans I get this error:
java.lang.NoClassDefFoundError: myrunnable/Main
Caused by: java.lang.ClassNotFoundException: myrunnable.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: myrunnable.Main. Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
How do i fix this?!
I have not used netbeans but from what I can see from your exception looks to me JRE may not be in the classpath. Have you checked that? For example when you create a project in eclipse it will automatically add JRE to the classpath.
OR
may be you need to explicitly compile/build before you can run. In eclipse it is auto too. When you run a simple.java it would have created the corresponding simple.class in the bin output folder.
Check those two areas you will be fine I think
Is there a main method defined in the class?
public static void main(String[] args) {
}
Also, is your Java JDK properly registered inside Netbeans?

Exception in thread "main" java.lang.ClassNotFoundException?

I cloned my project from GitHub and now I am not able to run my project.
package edu.test.algorithm;
import edu.test.algorithm.Global;
public class Main {
public static void main(String[] args) {
Global global= new Global();
global.runGlobal("-----url");
}
}
I am getting this error:
Exception in thread "main" java.lang.ClassNotFoundException: edu.test.algorithm.Global.Main
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
Any idea what's causing this error?
EDIT
I checked this question Intellij suddenly throwing ClassNotFoundExceptionbefore i posted here and it doesn't give me any solution.. Why marking my question as duplicate since that one isn't solving my problem?
Maybe your Ide is wrong run/debug configuration
Main -> Edit Configuration
enter image description here
Check your IDE run configurations and Class
https://www.jetbrains.com/help/idea/2016.2/creating-and-editing-run-debug-configurations.html
The exception message hints that you have used the wrong class name when launching the program.
You are (presumably) using edu.test.algorithm.Global.Main as the class name. However you have actually declared Main in the package edu.test.algorithm which means that its full classname is actually edu.test.algorithm.Main.
If you didn't do this explicitly, it is possible1 that you changed the Main classes package and neglected to change (or remake) the launcher config.
1 - I'm not familiar with Intellij ... but I know you can run into this kind of problem with Eclipse.

Categories