Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "ClassNotFoundException" in IntelliJ IDEA

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?

like image 973
Aido Avatar asked Jun 25 '13 14:06

Aido


People also ask

How do you fix IntelliJ could not find or load main class?

2) "Run" menu -> " Edit configuration " -> delete the profile -> add back the profile ("Application" if it's a Java application), choose your main class from the "Main Class" dropdown menu. 3)"Build" menu -> " Rebuild Project ". Show activity on this post. Then open the project with IntelliJ.

What causes ClassNotFoundException in Java?

ClassNotFoundException is a checked exception which occurs when an application tries to load a class through its fully-qualified name and can not find its definition on the classpath. This occurs mainly when trying to load classes using Class. forName(), ClassLoader. loadClass() or ClassLoader.

How do I access classpath in IntelliJ?

find your project or sub-module and click on it to highlight it, then press F4, or right click and choose "Open Module Settings" (on IntelliJ 14 it became F12) click on the dependencies tab. Click the "+" button on the right and select "Jars or directories..." Find your path and click OK.


2 Answers

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

like image 171
Mateusz D. Avatar answered Sep 24 '22 02:09

Mateusz D.


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.

like image 35
Trionet Avatar answered Sep 27 '22 02:09

Trionet