0

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)

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Mano,When I am executing the class file using java.exe ...its again giving me an error ...can u please assist....just one more time :)... C:\Program Files\Java\jdk1.8.0_91\bin>java C:\myData\HelloWorld Error: Could not find or load main class C:\myData\HelloWorld
You don't call it like that. You call it via its fully qualified classname e.g. java com.example.HelloWorld. You'll have to replace my example classname here with your own. Bear in mind, you may need to point java at where the class file is e.g. java -cp directory/where/classfiles/exist com.example.HelloWorld
1

-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.]

Comments

1

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"

1 Comment

Hi AR, I tried the quotes but still giving.. javac: no source files Usage: javac <options> <source files> use -help for a list of possible options
0

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

Please enclose path in "" (double quotes)

1 Comment

Thanks Anand for taking out time and replying ..but it was the wrong syntax I was using ...this worked fine ...javac -sourcepath "C:\my Data" "C:\my Data\HelloWorld.java"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.