1

I imported org.junit related jar file and I used IntelliJ IDEA to compile my java file to class file successfully. However, when I tried to used 'javac **.java' to compile in Mac terminal, it reported the error message "package org.junit does not exist". It seems java through the Mac did not detect the packages I imported. I have Spent a day on this but still could not solve it. Thank you if you have any clue.

S.Daineko
  • 1,790
  • 1
  • 20
  • 29
April
  • 11
  • 1
  • 1
    The package might be provided by IDE when run on intellij, try adding the dependency to your dependency management tool (gradle, maven, ant or whichever it is) – vizsatiz May 21 '20 at 05:28

2 Answers2

1

If you compile from command line use -cp option for javac to include libraries that are not in current directory to the compile classpath. Consult the javac documentation for more information.

Also see this thread Including jars in classpath on commandline (javac or apt)

Andrey
  • 15,144
  • 25
  • 91
  • 187
0

The JDK only contains the core runtime java classes, extensions (e.g. awt), and classes inside your application class path.

After you compiled junit jar, no classes appeared in this chain.

To use external libraries, you must use the build automation tool (maven or gradle), put the JUnit dependency there, and then run your application as a maven (or gradle) project. After installation this approach has 1 step more than just using javac.

Maven instruction(simpler preferred method): https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Gradle instruction: https://docs.gradle.org/current/userguide/getting_started.html

S.Daineko
  • 1,790
  • 1
  • 20
  • 29