12

I creating a program to work with databases and I am getting the following error when compiling in IntelliJ IDEA. Does anyone why this is happening and how I could solve it?

enter image description here

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
urpi
  • 289
  • 1
  • 2
  • 8

6 Answers6

29

The error that you get occurs not on complilation, but when you try to run your application. It happens because Java was not able to find Table.class file inside db subdirectory of the project output directory (classpath).

It can happen for multiple reasons:

  • wrong main class selected in the run/debug configuration
  • Table.java is excluded from compilation (by accident or intentionally because it contained errors and you wanted to skip it while working on other code)
  • class not compiled because Build step is excluded from from Before launch steps in the Run/Debug configuration
  • project is misconfigured and there is no Source root defined for the directory containing db subdirectory
  • Table.java has incorrect package statement or is located/moved to a different package
  • project path contains a colon : on Mac/Linux or semicolon ; on Windows, it's used to separate the classpath and will render the classpath invalid. See this thread for details. Note that Finder on Mac may display colons in the path as slashes.
  • the jar may not execute if one of the dependent jars is digitally signed since the new artifact will include the partial signature of the dependency. See this answer for more details.
  • In project structure make sure you have the right Java version for compile.
  • there is a known bug that sometimes a Java project created from the Command Line template doesn't work because .idea/modules.xml file references invalid module file named untitled104.iml. Fix the module name manually or create a project from scratch and don't use a template.
  • on Windows "Beta: Use Unicode UTF-8 for worldwide language support" Region Setting is enabled. See IDEA-247837 for more details and workarounds.
  • When IntelliJ IDEA is configured to store module dependencies in Eclipse format source root configuration is lost due to a known bug. Configure the module to use IntelliJ IDEA format dependencies as a workaround.

In a properly configured project and with the correct run/debug configuration everything works just fine:

Run

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • Thanks! Now it is running, but it gives me a new error. This error says Error java: invalid source release: 1.9. Do you know why this might be happening? – urpi Mar 08 '17 at 00:24
  • Thanks for the help! It works now! Can I know why the down vote? – urpi Mar 08 '17 at 03:31
  • 2
    I didn't downvote your question (I actually upvoted it), but a lot of people here think that it's off-topic, while FAQ clearly states that questions about "software tools commonly used by programmers" are perfectly valid. Posting screenshots in the question instead of the text of the actual errors is another common reason for downvotes. – CrazyCoder Mar 08 '17 at 03:34
  • I had a "/" in my file path. Took me 5 hours to realize :-( – AdamHurwitz Sep 26 '17 at 20:18
  • 1
    I had the wrong java version loaded. Went to File -> project structure -> project sdk -> select correct version. I have both 9.0 and 1.8 installed, and it took 9.0 by default. The version here had to be the same one as the one in run configuration. – sparkonhdfs Apr 05 '18 at 17:48
  • I had the same problem as @sparkonhdfs while running tests in Intellij. From terminal using `mvn` directly the test worked as the `JAVA_HOME` was set correctly to 1.8 but in Intellij the version was 10. – bogdan.rusu Jan 16 '20 at 07:53
  • "project path contains a colon : on Mac/Linux or semicolon ; on Windows"...That's exactly how it's supposed to be. This is a list of *mistakes*, so I think it might be confusing to include items that are correct as-is. – EntangledLoops Feb 17 '20 at 01:35
  • NOT TRUE: "In a properly configured project and with the correct run/debug configuration everything works just fine:" [previous comment from above] This worked perfectly well in Eclipse. When we transferred over to IntelliJ we get the error: "Error: Could not find or load main class" – user2782 Mar 12 '20 at 23:29
  • @user2782 could you please share any sample project that doesn't work for you so that I can help you with this problem? – CrazyCoder Mar 13 '20 at 02:22
4
  • the jar may not execute if one of the dependent jars is digitally signed since the new artifact will include the partial signature of the dependency. See this answer for more details.

I must again emphasis the point CrazyCoder has here.

The (Oracle) JVM used to throw a SecurityException when you tried to run a Jar-File containing broken signatures. This made sense from a "What's wrong"-Point of view.

That is no longer the case. They are indeed throwing ClassNotFoundExceptions now - even if the class is right there in the file (no matter if it is in the default package/toplevel or way down in a nested package structure).

2

Here's what worked for me:

I deleted .ide folder, .iml file. And all other auto generated files by intelliJ then restarted my ide and I was asked if I want to make my project run with maven that's it.

Obviously I said yes :)

Saikat
  • 14,222
  • 20
  • 104
  • 125
1

This is a known bug in the IntelliJ idea. To fix this I just deleted the .iml and the .idea and restart the IDE. It works for most of the cases

Edit: The files will be in the project directories.

1

In my case the default console app template works only if the project folder path does not contain underscore (_) in it. Underscore brings the error

Error: Could not find or load main class com.company.Main
Caused by: java.lang.ClassNotFoundException: com.company.Main

IntelliJ IDEA 2021.3.1 (Ultimate Edition) Build #IU-213.6461.79, built on December 28, 2021

Burst
  • 689
  • 7
  • 15
0

If you've tried everything else that others have suggested (deleting .idea folder, rebuild, etc) there's another place to check, especially if you've built an artifact jar. When you first build an artifact jar, IntelliJ adds a folder: META-INF to src directory. in it is a single file: MANIFEST.MF which has info pointing to the Main-Class for Java to find. If you've refactored your project package, unfortunately IntelliJ does not update this file with the new changes. My MANIFEST.MF has the following correct content:

Manifest-Version: 1.0
Main-Class: org.umoja4life.fatashibackend.MainKt

Where "org.umoja4life.fatashibackend" is the package name, and "MainKt" is IntelliJ's constructed name for a (pseudo) "Main Class" because fun main() has been defined in file "main.kt" in the package directory.

Newbies: btw, This will be confusing for you because there should be no actual "class Main {}" definition despite the error message stating there should be.

Before I discovered this file and after trying everyone else's suggestions, I found it quickest to just have IntelliJ start a project (with correct package name!), initialize it with a trivial main.kt having:

fun main() { println("hello world!") }

run and test that; then, I added back in all my other files, rebuilt, ran, and tested it. Apparently IntelliJ has some secret state information stored somewhere which doesn't get correctly updated if your refactor your package name for an already running project and jar.

dsaronin
  • 687
  • 5
  • 8