1

I have a problem with running my jar execution file built by Intellij Idea Artifacts in command line. Error message is

NoClassDefFoundError: io.mattmoore.kotlin.playground.cinterop.Greeter

But it worked when I use the IDE to execute "RUN".

Do you have any Idea why is that happening?

Highly appreciated if someone could give me a hint about how to fix it.

Intellij Idea project

Harrison

PHPirate
  • 7,023
  • 7
  • 48
  • 84

1 Answers1

0

IDE knows better where to find dependencies, but once you export .jar file, you need to manually ensure that no dependency is missing
(and that all get loaded).

Example:

To run MyProgram.jar, place all your dependencies inside of libs directory (which you need to create beside it), then use command like:

java -cp 'MyProgram.jar:libs/*' my_package.MyMainClass

OR for Windows (use semicolon)

java -cp 'MyProgram.jar;libs/*' my_package.MyMainClass

Note that dependency means other .jar files, for example, the one that defines io.mattmoore.kotlin.playground.cinterop.Greeter class.

See also How to call ".jar” with additional classpath option?

Top-Master
  • 7,611
  • 5
  • 39
  • 71
  • If I remember correctly, Intellij copies all dependencies for you automatically into some directory which's name I forgot, so use that name instead of `libs` (I mean, this is how you run Java without IDE, and adding artifacts to IDE does not change it!) – Top-Master Aug 21 '21 at 05:07
  • Thanks, I follow your suggestion, but still got same error as shown below: $ ls -l total 3456 -rw-r--r-- 1 xxx staff 3049 Aug 21 10:53 kotlin-cinterop-talk.main.jar drwxr-xr-x 3 xxx staff 96 Aug 21 13:14 libs ls lib* \greeter-klib-jvm.jar $ java -cp 'kotlin-cinterop-talk.main.jar:libs/*' io.mattmoore.kotlin.playground.cinterop.Greeter 錯誤: 在類別 io.mattmoore.kotlin.playground.cinterop.Greeter 中找不到主要方法,請定義主要方法為: public static void main(String[] args) 或者 JavaFX 應用程式類別必須擴充 javafx.application.Application – Harrison Lou Aug 21 '21 at 05:18
  • I am using mac os version 10.15.6, thanks – Harrison Lou Aug 21 '21 at 05:24
  • Only classes that define valid `main` method can be passed in command line, but as I think you already have `main`-class somewhere and is linked in manifest, simply run `java -cp 'kotlin-cinterop-talk.main.jar:libs/*'` (without class name); and I hope you are not trying to run an artifact? (I mean, your `.jar` name is strange) – Top-Master Aug 21 '21 at 05:41