Question

Trying to understand command line compiling but having issues working with classpath, "javac: file not found: Hello.java"

  • directory structure: Users/thered/Desktop/src
  • Hello.java has default package.

These WORK from src directory:

thered-MacBook-Pro:src thered$ javac Hello.java
thered-MacBook-Pro:src thered$ javac -cp . Hello.java
thered-MacBook-Pro:src thered$ javac -cp ../src Hello.java

These FAIL from Desktop directory:

thered-MacBook-Pro:Desktop thered$ javac -cp /src Hello.java
thered-MacBook-Pro:Desktop thered$ javac -cp ./src Hello.java
thered-MacBook-Pro:Desktop thered$ javac -cp /Users/thered/Desktop/src Hello.java
Was it helpful?

Solution

Classpath has nothing to do with where your source files are located, it points to where necessary already compiled class files and jar files are located.

If you want to be in the Desktop dir and compile src/Hello.java, just do;

javac -cp ./src src/Hello.java

or plain

javac src/Hello.java
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top