Intellij suddenly throwing ClassNotFoundException

2019-01-17 06:52发布

问题:

I'm at a complete loss here. I have a project on an external hard drive called LenseProject. Inside LenseProject, I have .idea, lib, Natives and SRC folders. I also have a number of text files for reading.

When I left work last night, this all worked fine. Coming in this morning, I'm met with:

Exception in thread "main" java.lang.ClassNotFoundException: QuadTest
    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) 

QuadTest being the main class.

Information that I think might be helpful:

  • If I comment out the imports, I get the appropriate error messages. (Cannot find symbol, etc).

  • I have 9 dependencies, located in lib\lwjgl-2.9.0\jar that are set up in Project Structure -> Libraries.

  • The module has LenseProject as the content root, and SRC as the source folder.

  • The language level is 7.0

  • I have the dependencies set to export in Project Structure -> Modules -> Dependencies.

  • In Compiler Output, I have it set to Use Module Compile Output Path, as Inherit Project Compile Output Path gave me "Cannot start compilation: The output path is not specified for module "LenseProject". Specify the output path in Configure Project.

  • I have VM option -Djava.library.path=Natives.

  • I can compile and run the program through command prompt no problem.

  • I was missing my configure options when I started it up this morning. I had to set the SDK again, and libraries.

回答1:

Open your Idea File --> Project Structure --> Modules ,and then choose problematic module,in the "Paths" tab change selected to "Use module compile output path ".



回答2:

I ran into a similar issue while writing unit tests. Everything would work at the command line but failed in IntelliJ. IntelliJ would successfully compile but not run the test.

Then I ran across a post on IntelliJ's blog: http://blog.jetbrains.com/idea/2014/03/intellij-idea-13-1-released/

anet says:

March 21, 2014 at 12:20 pm

You may remove the existing junit dependency and allow IDEA to add JUnit library for your from scratch.

New junit doesn’t bundle hamcrest anymore but still depends on it on runtime.

Thanks, Anna

  • See more at: http://blog.jetbrains.com/idea/2014/03/intellij-idea-13-1-released/#sthash.2KNQuwZ5.dpuf

I removed JUnit from my project settings and let IntelliJ add it back. Then things worked fine.



回答3:

In my case, the problem was I reused the "out" directory for program output. I solved it by redirect the output to another folder.



回答4:

I had similar issue. A new dependency was not being found when running tomcat. The problem was it wasn't being deployed to /WEB-INF/lib. After half a day banging my head on the desk I found this YouTube video that fixed it.

Essentially I needed to add the dependencies from module settings-> artifact->MyWar->Output Layout tab. Under available elements open your projects folder. If there are maven dependencies listed there, select them. Then right click->Put into /WEB-INF/lib



回答5:

I suggest confirming your Run Configuration as follows:

Toolbar: Run->Edit Configurations..., confirm your Configuration is correct.

Hope it helps.



回答6:

I know its a bit old post but for me helped by going to Run-->Edit Configurations--> In Application Selected your main code file --> Configuration on the right panel. Checked "Use alternative JRE:" and selecting JDK folder called jre(.../Java/jdk.x.x.x_xx/jre). Apply and OK. I am really new with intelliJ but that solved my problem hope it helps someone.



回答7:

My problem was, that all this renaming the project-structure, my Maven dependencies didn't mach.

After changing the maven dependencies in the pom file, it worked.



回答8:

File -> Project Structrure -> Project. Change the Project Language level from "SDK default" to Actual version that you are using.

This is weird, but solves the problem.



回答9:

Try going to Preferences -> Compiler and select Eclipse, rather than using javac.