Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javac -sourcepath problem

 
W. Schaible
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
W. Schaible
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic