BlueStar
BlueStar

Reputation: 59

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)

Upvotes: 0

Views: 1812

Answers (4)

M A
M A

Reputation: 72844

You need to place the source path in quotes so that the command line processes it as a single argument. The source path must also be the directory in your case, not the file:

javac -sourcepath "C:\my Data"

Upvotes: 1

ManoDestra
ManoDestra

Reputation: 6473

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.

Upvotes: 3

Anand Kulkarni
Anand Kulkarni

Reputation: 87

Path C:\my Data\HelloWorld.java has space in it hence the error.

Please enclose path in "" (double quotes)

Upvotes: 0

WPrecht
WPrecht

Reputation: 1382

-sourcepath is a PATH, you are giving a file name that's not a java file, that's not valid. From the docs:

-sourcepath sourcepath

Specify the source code path to search for class or interface
definitions. As with the user class path, source path entries are
separated by colons (:) and can be directories, JAR archives, or ZIP
archives. If packages are used, the local path name within the 
directory or archive must reflect the package name.

[EDIT: OP changes the file name to .java in the question, as the other answer noted, it needed quotes.]

Upvotes: 1

Related Questions