aspose file tools*
The moose likes Beginning Java and the fly likes sourcepath and classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sourcepath and classpath" Watch "sourcepath and classpath" New topic
Author

sourcepath and classpath

ZahidM Khan
Greenhorn

Joined: Apr 06, 2012
Posts: 8
Operating System:
Windows

Directory structure:

C:\OCJP\src\com\domainName\projectName\ClassOne.java
C:\OCJP\classes

Java source file:
class ClassOne{
package com.domainName.projectName;
public static void main()
{
System.out.println("Hello world...");
}

}

classpath setting
C:\> echo %classpath%
C:\>C:\OCJP\src;C:\OCJP\classes


Logical Concept

The compiler looks in the class path for both a source file and a class file, recompiling the source (and regenerating the class file) if it is newer. http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/javac.html

Steps to reprodce problem

Javac Case 1:
C:\>javac com\domainName\projectName\ClassOne.java
C:\>javac: file not found: com\domainName\projectName\ClassOne.java
Usage: javac <options> <source files>
use -help for a list of possible options

C:\>javac -classpath com\domainName\projectName\ClassOne.java
C:\>javac: file not found: com\domainName\projectName\ClassOne.java
Usage: javac <options> <source files>
use -help for a list of possible options

C:\>javac -classpath src com\domainName\projectName\ClassOne.java
C:\>javac: file not found: com\domainName\projectName\ClassOne.java
Usage: javac <options> <source files>
use -help for a list of possible options

Javac Case 2:
C:\>cd C:\OCJP\src
C:\OCJP\src>javac com\domainName\projectName\ClassOne.java
C:\OCJP\src>

compilation was successful


Java Case 3:
C:\>java ClassOne
C:\> Hello world...

Java Case 4:
C:\>cd C:\OCJP\classes
C:\OCJP\classes>java ClassOne
C:\OCJP\classes> Hello world...

Problem statement
I believe that javac command in Javac Case 1: should work because classpath variable is set to to the C:\OCJP\src directory and our Logical Concept says that compiler looks for both source and class files in class path.
Its strange that when we run java command it can find the .class file using classpath but when I try to compile the code using javac it seems like it doesnt use the class path to search for com directory and then eventually the source file ClassOne.java.

I have tried same thing using -sourcepath variable as well and it has same problem.

Please can someone tell me how can I compile it like C:\>javac com\domainName\projectName\ClassOne.java and whats wrong with Javac Case1: ?
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
ZahidM Khan wrote:I believe that javac command in Javac Case 1: should work because classpath variable is set to to the C:\OCJP\src directory and our Logical Concept says that compiler looks for both source and class files in class path.

This is the problem with taking a single sentence out of context. You need to reread the article you linked to and understand when javac uses the classpath to search for source files.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39425
    
  28
I hope that is not a system classpath, because a system classpath usually causes more trouble than it is worth. Always set the classpath individually for each application.
ZahidM Khan
Greenhorn

Joined: Apr 06, 2012
Posts: 8
Its a local class path; I have set it through command prompt.
c:\>set classpath=C:\OCJP\src;C:\OCJP\classes
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39425
    
  28
Good thing, having the classpath local. That is a good way to set it for the lifetime of that command window.
ZahidM Khan
Greenhorn

Joined: Apr 06, 2012
Posts: 8

This is the problem with taking a single sentence out of context. You need to reread the article you linked to and understand when javac uses the classpath to search for source files.


Hi Stuart A. Burkettart,

I have already read this article couple of times but it doesn't make me sense anymore thats why I posted this question. However, on your reply I have read it again and it says
When the compiler needs type information, it looks for a source file or class file which defines the type. The compiler searches first in the bootstrap and extension classes, then in the user class path. The user class path is defined by setting the CLASSPATH environment variable or by using the -classpath command line option. (For details, see Setting the Class Path). If you use the -sourcepath option, the compiler searches the indicated path for source files; otherwise the compiler searches the user class path both for class files and source files

Set the user class path, overriding the user class path in the CLASSPATH environment variable. If neither CLASSPATH or -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.
If the -sourcepath option is not specified, the user class path is searched for source files as well as class file
.

Please can you point out which information I am missing? I need help.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
ZahidM Khan wrote:
When the compiler needs type information, it looks for a source file or class file which defines the type...
.

The compiler will only need type information if it is compiling a Java source file and it comes across a type it doesn't recognise. Therefore, before it can need type information, it has to have actually started compiling a source file. i.e. it uses the classpath to search for source (or class) files that define the type it is looking for. It does not use the classpath to find the source file you have specified on the command line.
ZahidM Khan
Greenhorn

Joined: Apr 06, 2012
Posts: 8
That makes sense. Thanks

But why does not it cause problem when we use 'java' command with file name and can find the .class file mentioned in command line?

Java Case 3:
C:\>java ClassOne
C:\> Hello world...
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
ZahidM Khan wrote:But why does not it cause problem when we use 'java' command with file name and can find the .class file mentioned in command line?

Java Case 3:
C:\>java ClassOne
C:\> Hello world...

You don't use the java command with a file name, you use it with a class name. And java looks for a file containing that class in the directories specified in the classpath.
So you are trying to execute a class called ClassOne. Your class path contains the directories C:\OCJP\src and C:\OCJP\classes. If there is a .class file in either of those directories containing a class called ClassOne then java is happy and it runs the program.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sourcepath and classpath