Error “ClassNotFoundException” in IntelliJ IDEA

2019-01-07 14:38发布

问题:

I made this simple program:

package main.java;

public class start {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

And got this error. I have NO idea what is happening,

"C:\Program Files\Java\jdk1.7.0_21\bin\java" -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 12.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_21\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext\zipfs.jar;C:\Users\Tim\IdeaProjects\Rust\out\production\Rust;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 12.1.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain java.start
Exception in thread "main" java.lang.ClassNotFoundException: java.start
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:188)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)

Process finished with exit code 1

Anyone know what is wrong?

回答1:

Probably your project structure was src/main/java/start.java but when you added it to IntelliJ you have set src as a source folder, so IntelliJ put main.java as a package.

In Project Setting (Ctrl+Shift+Alt+S) → Modules → Sources tab set src/main/java as Source Folder. Then in your simple program change package to whatever you like (e.g. my.test). After that if IntelliJ reports any error in line with package quick fix with Alt+Enter should help



回答2:

I had this problem and couldn't solve it with any of these solutions. However I think the problem was that I was using 'open' to create a project from a directory with some java files in. Instead I had to 'create new project' with the same directory as root.

Make sure you delete any '.idea' directories and '.iml' files when 'create new project' since it will otherwise complain about a project already being in the given directory.



回答3:

As mentioned before you should check File → ProjectStructure → ProjectSettings → Modules → Sources - your folder must be marked as a "Source". Also you should update your Run → EditConfigurations → Configuration → Main class to correspond with your package



回答4:

In my case the path to the project had some characters such as : \ /

ex: C:\something\other:thing\aaa\project , and that's why it didn't worked. Moving the project to a path that doesn't have those characters fixed the errors.



回答5:

Just for the future: I had unicode characters in the folder name like "Course name"/Tehtävät/Project and the problem was in "ä". When i changed it to "a" the program started to work.



回答6:

For me, in IntelliJ-Idea 2016, the problem was that in Project_structure/modules, in the Path tab you have to check "Inherit project compile output path", rather than "Use module compile output path"



回答7:

I falled down this problem after a recent re install of my intellif community edition.

IntelliJ IDEA 2016.3.2 Build #IC-163.10154.41, built on December 21, 2016 JRE: 1.8.0_112-release-408-b6 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o om my laptop asus N73SV with Ubuntu 16.04

The Context is: I was trying to restart Intellij projects previously running fine. That projects where done with intellij CI, AND THEY WHERE located on my freebox server.

I've tryed all solutions proposed here, but without result. At the end I just: Close the project. Shutdown Intellij. copy / paste the entire project from my freebox server to my system folder. Re start Intellij, open the project previously paste into my system folder. Rebuild the projet from the /Build/Re build menu. And finaly Run my project. And !!! DONE...It works!!

My suggest is: Is there a problem with pathname lenght or characters contented in that pathname to the project. Indeed /run/user/1000/gvfs/smb-share:server=freebox,share=disque%20dur/blaBlaToMyFileProjectFolder The full pathname contents 127 caracters among :,-_/%=

It that the tric?

Hope it's help. Sorry for my broken english. I'm french ;-) Oliver



回答8:

I also had same problem but I couldn't find solution on web so tried rectifying myself, after reading so many blogs i understood the concept, In my case everything was fine as I have download code from git but my IDE intellij couldn't recognise maven dependencies and that's why intellij was throwing this error, my pom.xml was having all dependencies but still intellij couldn't able to recognise it. I navigated to maven window at right in intellij and added path of the project in which I was working after adding it maven loaded all the dependencies and my code worked fine.So basically only three steps can make you resolve it: 1. Open maven window in intellij. 2. Click "+" and add path where your prjojects pom.xml file is present. 3. Click Enter. Now you can run your project. Happy coding!!



回答9:

Your package is main.java, you pass the argument com.intellij.rt.execution.application.AppMain java.start, and you exception says that that can not found java.start. And this is correct.

Try to compile it using only console. Leave the IDE for the beginning, invest you time to understand the compiler not how the IDE works.