BlueStar BlueStar - 6 months ago 65
Java Question

Compiling java program using javac from command prompt

I have new laptop on which I have installed jdk1.8.0_91 and jre1.8.0_91.
Both are in the "C:\Program Files\Java" folder.
I have NOT set any classpath or any environment variables.
I wrote a a HelloWorld.java program and saved it in "C:\my Data" folder.
I then went to Command Prompt using cmd.
Then I changed the current directory to "C:\Program Files\Java\jdk1.8.0_91\bin" ..since here is the javac.exe

and then tried to compile my HelloWorld program and its giving the following error -

C:\Program Files\Java\jdk1.8.0_91\bin>javac -sourcepath C:\my Data\HelloWorld.java
javac: invalid flag: Data\HelloWorld.com
Usage: javac <options> <source files>
use -help for a list of possible options


I am not sure whether I am correctly using the "sourcepath" or not...

How should I tell the compiler where my source file is ?(and I want to resolve this without setting any classpath or any environment variables)

Answer

Use this instead...

javac -sourcepath "C:\my Data" "C:\my Data\HelloWorld.java"

The sourcepath parameter allows you to specify the DIRECTORY where source files will be found. As per the javac command line output:

-sourcepath Specify where to find input source files

The parameter after that specifies the actual Java files to compile. You will need " around the parameters, given that your paths have spaces in them. Avoid spaces in your paths where ever possible to avoid this issue.

Comments