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?
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
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:
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)db
subdirectoryTable.java
has incorrect package
statement or is located/moved to a different package:
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..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.In a properly configured project and with the correct run/debug configuration everything works just fine:
- 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).
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.
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 :)