1

I'm on macOS Sierra, FYI.

So I'm trying to set a classpath variable, which has been done using: nano .bash_profile

and I'm able to set the variable as follows:

# setting CLASSPATH for Hello World as a test 
CLASSPATH="/Users/jonvanderlaan/NetBeansProjects/HelloWorld/src/HelloWorld"
export CLASSPATH

# setting PATH for Java 1.8.0
PATH="/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/bin/java:$
export PATH

When I echo the classpath, it shows me the correct classpath. However, when I try to compile and run a .java in that folder, it doesn't work.

javac HelloWorld.java
javac: file not found: HelloWorld.java
Usage: javac <options> <source files>
use -help for a list of possible options

I am attaching a picture of what my directories look like.

Can someone tell me what my problem is here?

(also I'm fairly new to programming and VERY new to the command line. First time posting here. Please be nice!)

(edited to fix problems...still not working)

vandy
  • 75
  • 7

1 Answers1

1

javac is the java compiler. try java -jar HelloWorld.jar to run your program.

Andbdrew
  • 11,788
  • 4
  • 33
  • 37
  • I get "Unable to access jarfile HelloWorld.jar" – vandy Dec 13 '16 at 20:07
  • 1
    @vandy Your working directory needs to be where the file is. – RaminS Dec 13 '16 at 20:32
  • what's the output of `ls`? – Andbdrew Dec 13 '16 at 20:39
  • @Gendarme I'm not in that directory. I may be misunderstanding the purpose of a classpath. Shouldn't I be able to be in any directory and the compiler searches the classpath when I compile? – vandy Dec 13 '16 at 20:56
  • @Andbdrew Awesome. So I must be in the correct directory to run the program, but if the program extends classes outside of that package, they must be specified in the classpath. Is that accurate? – vandy Dec 13 '16 at 21:34
  • @vandy You give the path name of the package when you import it, but it is probably relative to the classpath. – RaminS Dec 13 '16 at 21:41