0

I have generated a jar using Intellji Idea but it throws a compile error: Could not find or load main class com.xxx.xxxxx

I guess it was caused by I did not explicitly set the class path in manifest properties. Any idea how to do it? Thanks!

enter image description here

crazymind
  • 169
  • 2
  • 11
  • Possible duplicate of [Cannot run jar file: Could not find or load main class Hello](https://stackoverflow.com/questions/39774012/cannot-run-jar-file-could-not-find-or-load-main-class-hello) – racraman Jun 13 '19 at 02:57
  • @racraman thanks, but not really. – crazymind Jun 13 '19 at 03:08
  • See https://stackoverflow.com/a/42200519/104891 for the sample project. – CrazyCoder Jun 13 '19 at 03:11
  • @CrazyCoder thanks but I need set the classpath not add an external library. – crazymind Jun 13 '19 at 03:20
  • Do you need to set classpath or the main class? Are you sure it's a compile error and not runtime error? – CrazyCoder Jun 13 '19 at 03:22
  • @CrazyCoder I've set the main class already. Not sure whether is compile error or run time error. – crazymind Jun 13 '19 at 03:28
  • Please share the [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – CrazyCoder Jun 13 '19 at 03:29
  • It's a runtime error if you get it when attempting to run the jar (eg by "java -jar xyz.jar"). I'm going with it being a runtime error :) The fact the error has the full package "com.xxx.xxxx" means the full package name has been picked up, so further classpath issues in the manifest shouldn't be a problem. Maybe check that the classes are in the jar (eg just rename to ".zip" to do so), as per the answer I linked to : "You need to specify that you want to add the compile output to the jar or else it will only have the manifest and not your actual classes. " – racraman Jun 13 '19 at 03:54
  • @ racraman Thanks, I changed it to zip file and find there is only one file :MAINFEST.MF. Do you know why? I follow the steps in this post: https://stackoverflow.com/questions/1082580/how-to-build-jars-from-intellij-properly/4901370 – crazymind Jun 13 '19 at 04:48
  • Use a build tool such a Maven or Gradle don't generate artefacts using your IDE directly. Intellij is a professional development tool and as such expects you to use professional development practices. If you want to learn to code, use an IDE designed for learning not an IDE designed for getting stuff done. – Boris the Spider Jun 13 '19 at 05:02
  • The module you've added may not have any compiler output, check the content and source roots for the module you are using in the artifact. – CrazyCoder Jun 13 '19 at 05:26
  • @CrazyCoder Thanks. I am not quite sure how to check compiler output. I rebuild the projects and it runs ok but I cant run the jar for same issue. I tried maven approach which throw an exception. – crazymind Jun 13 '19 at 06:44
  • See https://www.jetbrains.com/help/idea/configuring-content-roots.html. Make sure the module you've added to the artifact has source roots which produce the output classes. – CrazyCoder Jun 13 '19 at 07:39

0 Answers0