Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compilation using classpath

 
J C upadhyay
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
J C upadhyay
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
J C upadhyay
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic