This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Java in General and the fly likes javac -sourcepath problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "javac -sourcepath problem" Watch "javac -sourcepath problem" New topic
Author

javac -sourcepath problem

W. Schaible
Greenhorn

Joined: May 09, 2012
Posts: 5
Hello. I'm using the javac compiler with Ubuntu Linux. I normally use NetBeans to compile and run programs but I'm practicing working from the command line. If I change directory to the directory of the java source file and run: javac <filename.java> it compiles the file, creates file <filename.class> and writes the class file <filename.class> to the source directory (because I didn't specify -classpath). This is exactly what to expect. Everything fine. The problem is when I'm in some other directory besides the source directory and I use the -sourcepath option to specify the location of the java source file it says file not found (source file). Specifically, if I enter:
javac -sourcefile home/bilsch/NetBeansProjects/HelloWorld/src Hello1.java it says it can't find file Hello1.java. I've tried every variation I can think of but I just can't get the -sourcepath option to work. Has anyone had similar experience using -sourcepath option? Any suggestions?

TIA. Bill S.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

Hello W. Schaible,

Welcome to CodeRanch!

Please make sure that file really exist at given path from your current directory. In your example, you are using relative path, and not absolute path. So, make sure that currently you are in parent directory of 'home'.

To avoid confusion, I would suggest to use absolute path (i.e. starting with '/').

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
W. Schaible
Greenhorn

Joined: May 09, 2012
Posts: 5
Hello, I have tried absolute paths and running from parent directory of relative path. Nothing works. The options -sourcepath and -classpath are just ignored like they aren't even there.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
W. Schaible wrote:... writes the class file <filename.class> to the source directory (because I didn't specify -classpath).

Firstly the classpath option has nothing to do with where class files are written to. It is used to tell the compiler where it can find class files that it needs in order to check the syntax of your code. If you want the class files to be written to a different directory, use the -d option.

W. Schaible wrote:The problem is when I'm in some other directory besides the source directory and I use the -sourcepath option to specify the location of the java source file it says file not found (source file). Specifically, if I enter:
javac -sourcefile home/bilsch/NetBeansProjects/HelloWorld/src Hello1.java it says it can't find file Hello1.java. I've tried every variation I can think of but I just can't get the -sourcepath option to work. Has anyone had similar experience using -sourcepath option? Any suggestions?

Someone had the same problem here. We couldn't get the sourcepath option to work there, but in the end discovered we didn't really need it. The following should work.
javac home/bilsch/NetBeansProjects/HelloWorld/src/Hello1.java


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
Welcome again.

Also confirm what the argument required for sourcefile is; I am not familiar with that option. Is it supposed to be a file, or a directory. You would appear to be pointing to a directory. It is often easier to move to the directory in question and use it as the current directory.
Have you got a system or user classpath set: try something like echo $CLASSPATH at a shell; it ideally would return a blank.
W. Schaible
Greenhorn

Joined: May 09, 2012
Posts: 5
based on the responses to my question I was able to make a script filethat uses two command line parameters that does the job. It finds the source file using a path made using the project name (first parameter) and the source file name (second parameter). Using the -d option I specified where to put the class file - that path being created using the project name (first parameter). Thank you all for your help. The script looks like this:

javac -d /home/bilsch/NetBeansProjects/$1/build/classes /home/bilsch/NetBeansProjects/$1/src/$2 - where $1 is the project name and $2 is the source file name
 
wood burning stoves
 
subject: javac -sourcepath problem