aspose file tools*
The moose likes Java in General and the fly likes Compilation using classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Compilation using classpath" Watch "Compilation using classpath" New topic
Author

Compilation using classpath

J C upadhyay
Greenhorn

Joined: Nov 24, 2011
Posts: 16
I have a file hello.java in an unnamed package. If i compile it using C:\exercise\ex1>javac hello.java, it compiles without any problem. However, if i try to compile it using classpath command it gives a file not found error.

I used the following command:

C:\exercise>javac -cp ex1 hello.java .


Why can't the compiler find hello.java in the specified package?[/size]
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 560
I think you're -cp option statement is incomplete. Shouldn't you specify the whole path? Refer to this page for javac's options. Look at the -sourcepath option while you're there.

In fact, you might want to reconsider your use of the CLASSPATH option (-cp) which overrides your normally set CLASSPATH, since it may cause you other problems. It seems the -sourcepath option fits your needs better.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
And welcome to the Ranch
J C upadhyay
Greenhorn

Joined: Nov 24, 2011
Posts: 16
The directory structure is:
C:\------|
|--exercise---|
|----ex1---|
|---hello.java
Here hello.java is in an unnamed package.

I tried the following and got the file not found error:

C:\exercise>javac -cp C:\exercise\ex1 hello.java (specifying absolute path in -cp)
C:\exercise>javac -sourcepath C:\exercise\ex1 hello.java (specifying absolute path in -sourcepath)

However,
C:\exercise>javac -cp ex1 ex1\hello.java , compiles without any complains.

Is it that source files in unnamed packages can only be compiled if path to .java file is given relative to the current directory irrespective of the classpath specified in the command?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

J C upadhyay wrote:I tried the following and got the file not found error:

C:\exercise>javac -cp C:\exercise\ex1 hello.java (specifying absolute path in -cp)

That's because the classpath is only used to find *.class files, not *.java source files. Since hello.java is not in the current directory (C:\exercise), the compiler can't find it.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
J C upadhyay
Greenhorn

Joined: Nov 24, 2011
Posts: 16
Thanks, Jesper

There is one more doubt. As Greg told me what if I use -sourcepath instead of -cp in the same command. Now, javac should be able to find the *.java , but still it gives the file not found error.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

If your program is in a package "ex1" (you have a statement: package ex1; in your source file), then you need to put the base directory of the package in the sourcepath (and in the classpath when running it). Not the package directory itself.

So this would be correct:

javac -sourcepath C:\exercise ex1\hello.java
java -cp C:\exercise ex1.hello
J C upadhyay
Greenhorn

Joined: Nov 24, 2011
Posts: 16
I understand , but as I have mentioned it in my first post, the program is in an unnamed package inside the directory ex1(i.e.the statement: package ex1; is not in my source file).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compilation using classpath