GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Compiling java program with cp switch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiling java program with cp switch" Watch "Compiling java program with cp switch" New topic
Author

Compiling java program with cp switch

Vishnu Khera
Greenhorn

Joined: Nov 29, 2008
Posts: 24

I'm unable to understand the way -cp switch is used with the javac command. I have looked at the documentations and online resources. I've properly checked environment variable for Windows 7 machine. Here is the image that explains my question:



http://tinypic.com/view.php?pic=efpibp&s=6



http://tinypic.com/view.php?pic=2wnpso4&s=6


I'm unable to understand the precise reason for 'no source file' error with -cp switch using the javac command. The Extend.java and TE.java are given below for your reference:



Akshay Madhuranath
Greenhorn

Joined: Nov 29, 2009
Posts: 20
Hey Vishnu,

CP is basically used to tell the path of your class files. Let say you have a folder called classes in c:/users/classes. Now you place your class file in this folder. For the JVM to understand where your class lies, you have to specify the class path. Same with jar files too. In your example, since you are using a package, when you compile the java file, it will create directories as you have specified in the package. Like this. {directory}/com/gonno/profile/pic/{your class files}..

Thanks,
Akshay
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

Vishnu, we use -cp for giving classpath.
Vishnu Khera
Greenhorn

Joined: Nov 29, 2008
Posts: 24
-cp path or -classpath path
Specify where to find user class files, and (optionally) annotation processors and source files. This classpath overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -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 also searched for source files.

If the -processorpath option is not specified, the classpath is also searched for annotation processors.

As a special convenience, a class path element containing a basename of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR.

For example, if directory foo contains a.jar and b.JAR, then the class path element foo/* is expanded to A.jar:b.JAR, except that the order of jar files is unspecified. All jar files in the specified directory, even hidden ones, are included in the list. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. The CLASSPATH environment variable, where defined, will be similarly expanded. Note: Depending of the configuration of your command line environment, you may have to quote the wild card character, for example, javac -cp "*.jar" MyClass.java.

From Oracle - javac

And Java Tutorials

These links do not explain why any random garbage is required after -cp switch for javac to work properly. (I've mentioned it in a call out bubble - in previously posted images - for your reference)
Vishnu Khera
Greenhorn

Joined: Nov 29, 2008
Posts: 24
For your reference:



[Thumbnail for IMG3.JPG]

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Those screenshots are illegible: please read this.
Vishnu Khera
Greenhorn

Joined: Nov 29, 2008
Posts: 24

I'm sorry . The screen shot says:

C:\Users\vk>echo %Path%
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files
\Del 1\DW WLAN Card;C:\Program Files\Java\jdk1.6.0_38\bin

C:\Users\vk>


These are the environment variables for 'Path'
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3606
    
  15
Vishnu Khera wrote:These links do not explain why any random garbage is required after -cp switch for javac to work properly. (I've mentioned it in a call out bubble - in previously posted images - for your reference)

The -cp switch doesn't require random garbage. It requires a list of directories and jar files that contain .class files representing the classes used by your source code.
If you type random garbage, one of two things will happen
1. If your source code doesn't use any external classes, then it will compile correctly. i.e. the compiler doesn't need to search for other classes so ignores the classpath setting.
2. If your source code does use external classes you will get compiler errors complaining about unknown symbols. The compiler will have assumed that your 'random garbage' is a directory name and will have tried to look in there for the .class files. As the 'random garbage' doesn't represent an actual directory, obviously no .class files will be found there.

Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
That PATH can cause errors. You should put our Java installation folder at the beginning of the PATH, because there might be another Java installation elsewhere. You can get problems if the javac installation you find is newer than the first java program. Such a problem cannot occur if you put the JDK installation first in the PATH. I do not know whether it makes any difference if you put the JDK installation in the system PATH or the user PATH.
Vishnu Khera
Greenhorn

Joined: Nov 29, 2008
Posts: 24
Joanne, thanks for your answer . I understand what -cp switch is for and do realize javac is not meant to compile complex projects. Campbell, thanks for your advise. I've changed user PATH and also set the system PATH to reflect JDK bin folder as the first statement.
 
 
subject: Compiling java program with cp switch