Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Classpath 1

Status
Not open for further replies.

mpsoutine

Programmer
Jan 6, 2003
87
0
0
US
Hi All,

Could anyone be kind enough to tell me how to check my CLASSPATH from the command prompt.

Soutine
 
What OS are you using?

If you are using windows and have a DOS prompt type
Code:
set classpath
and this will show you what your classpath is. Just typing
Code:
set
will list all your environment variables in DOS.

Or you could do it via a java app. I believe (from memory) that it is
Code:
String cp = System.getProperty("classpath");

or something similar. ----------------------------------------
There are no onions, only magic
----------------------------------------
 
Hi mpsoutine:

Another way is with the echo:

DOS: echo %CLASSPATH%
UNIX: echo $CLASSPATH

This way you can avoid the difference from the shells of unix (bourne, cshell, etc).

Hope it helps. Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
Hi Everyone,

Thanks for your help. Pedro I did try "echo %CLASSPATH% at the DOS command prompt but it returned %CLASSPATH% I did set the classpath at the DOS prompt by entering:

set CLASSPATH = C:\javaprograms.

Any idea why this may be happening.

Soutine
 
Hi mpsoutine:

I repeat the question of jfryer, what OS are you using?

I have Windows ME, and I get the classpath this way:

echo %CLASSPATH%

If the classpath variable is not set, it must print

Echo is on

It depends on your OS, it is possible that in windows 2K or XP it works different. In those operating systems, there are two ways to reach the command prompt, but with different results:
1) typing "command" in the execute window.
2) By the direct link in programs->accesories->cmd (or typing cmd in the execute window)

Each DOS console is different, it is easy to work in the cmd console and the echo could work different.

If you set an environment variable in the DOS console, its scope would be that console, if you want to set the variable for the system, you can set it in the AUTOEXEC.BAT file (in w95-98-ME) or you can set it in the system properties in w2K-XP. To set it in the system properties you must open the control panel and double click System (or type the windows key <between ctrl and alt> and the pause key at the same time). There you must go to advanced and Enviroment variables. There is a list with all the enviroment variables, including the classpath. If it is not present you can create it.

Hope it helps.
Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
cheers to Pedro

This is a very good post for all beginners who are unaware or unable to set the classpath, which is the first hurdle for all Java Programmers

-
Aravind
 
Hi Everyone,

I'm using Windows 2000. I know there is another thread related to packages on this forum but I'm still a little confused on the relationship between CLASSPATH, packages and compiling.

I keep all my java files in: C:\JAVAPROGRAMS

I create a package:

package com.myCompany;

public class HelloWorld
{

public static void main(String[] args)
{
System.out.println(&quot;Hello&quot;);
}//end of main

}//end of class HelloWorld constructor

I save the java file in a sub directory of JAVAPROGRAMS ie C:\javaprograms\com\myCompany

At the DOS prompt: C:\javaprograms\com\myCompany
I type: javac HelloWorld.java and the program compiles fine.


At the DOS prompt: C:\javaprograms\com\myCompany
I type: java HelloWorld

I get Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: com/myCompany/HelloWorld

I set the classpath at the DOS prompt as follows:

C:\>set classpath = c:\javaprograms
I also tried set classpath = c:\javaprograms\com\myCompany



QUESTION????????????????????????????????????????????
1)Why can't I get this to run.

2)Are packages just for importing class files?

3)Where in the directory structure do I have to be to run this program. Can I run it from: C:\ or do I need to be in C:\javaprograms\com\myCompany

Soutine
 
Try this:

set classpath = &quot;.;C:\Javaprograms&quot;

I think that has to do with the current directory, and the default package, but I am not sure.
 
Hi Egobbitz,

I did try it both ie with and without quotes. No luck.

Soutine
 
Assuming:

Your package is com.myCompany
You have the directory structure :\javaprograms\com\myCompany, in which the file HelloWorld.class can be found.

In order to run your app:
[1] Add c:\javaprograms to your classpath
[2] Type java com.myCompany.HelloWorld

Hope this helps. Cheers, Neil
 
Hi Nell,

Set the classpath to c:\javaprograms at the DOS prompt and verified it at the DOS prompt by using set. Typed java com.myCompany.HelloWorld at command prompt: c:\javaprograms\com\myCompany. Still no luck. Is setting the classpath at the command prompt the correct location or should I be setting it in the Environmental Variables location?

Soutine


 
Hi all:
I was about to write a FAQ about this classpath and package stuff, but jfryer did it for me. He posted a very clear explanation of this process in the following thread:

thread269-447080

Tawrn gives another useful tip.
Congratulations to jfryer for a good post, I hope it will answer all questions in this thread.
Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
Hi again:

Sorry Deepanwita, I forgot your question. In W2K you can type &quot;set&quot; to see the a list of the most common enviroment variables. A windows 2000 server uses the same file system of NT and I think the method for 2000 will work in NT.

You can find a lot of more information about this in this url:


Hope it helps. Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
Hi,

I bert, a java programmer.

1)
your java file or classfile must be in the directory com/mycompany
If you make a directory c:\javaprograms\com\myCompany and put Helloword.class in that directory

2)try this one on the command prompt:

java -classpath c:\javaprograms com.myCompany.HelloWorld

normally it should work.

I tried it myself. This is my sequence

1)I tried this java code:

package bert;

import java.io.PrintStream;

public class HelloWorld
{

public HelloWorld()
{
}

public static void main(String args[])
{
System.out.println(&quot;Hello by BS&quot;);
}
}

I saved the HelloWorld.java file in the directory C:\temp\bert

2)
on the command prompt (on c:\):

C:\javac.exe c:\temp\bert\HelloWorld.java

3) call your class file from C:
C:\java -classpath c:\temp bert.HelloWorld

'Hello by BS' will be displayed on the command prompt.

-classpath (or -cp) adds the next parameter to your classpath see java -?
So I add c:\temp to my classpath. And java.exe starts from there searching the main class in the path bert\HelloWorld.
So from c:\temp the path bert\HelloWorld.class exists

I Hope this works.

Please report the result.

cheers
Bert
java programmer
Deinze, Belgium
 
Hi Bert,

Thanks! I was able to get it to work. I'm still confused about setting the classpath. In your example you do not use the set Classpath option. My understanding was that if you use the classpath option you can also execute the program. If I do use this option ie. set CLASSPATH = C:\JAVAPROGRAMS does it matter which directory I am in to execute my HelloWorld program?

Should I be in C:\javaprograms\com\myCompany or C:\?

Neil suggested in a previous thread:

&quot;Assuming:
Your package is com.myCompany
You have the directory structure :\javaprograms\com\myCompany, in which the file HelloWorld.class can be found.
In order to run your app:
[1] Add c:\javaprograms to your classpath
[2] Type java com.myCompany.HelloWorld
Hope this helps. Cheers, Neil&quot;

Any insight into what happened?
 
Hi,

you can execute your java program from everywhere: c:\ or c:\xyz\123
BUT,
while calling the java.exe you must add the path from where java can find your clas file with the method main()

so,

if your current directory is c:\xyz\123 and you will call java.exe, you add the path c:\javaprograms to the classpath off your java.exe by the parameter -classpath.
From there (=c:\javaprograms) java.exe search for the directory com\myCompany. There java.exe searches for the class-file HelloWorld.class.

I hope you get it.

greetz,
Bert
 
Think of it like this. If you have a file called Example.java residing in:
Code:
c:\java
    --> source

The file contains:
Code:
package com.toolkit;
class Example {
  public static void main(String[] args) {
    System.out.println(&quot;Hello&quot;);
  }
}

You also have a directory structure:
Code:
c:\java
  --> classes

Now, try compiling your source file using:
Code:
javac -d c:\java\classes Example.java

The -d option ensures that the compiled class file appears in a directory structure matching its package declaration, so that you get:
Code:
c:\java
  --> classes
    --> com
      --> toolkit
        --> Example.class

Now to run this, type:
Code:
java -cp c:\java\classes com.toolkit.Example

Notice how the classpath does not point to the directory in which the class resides. Instead it points to the directory in which the directory 'com' resides, which is where the directory structure corresponding to our package declaration begins.

You can compile your source file without using the -d option, but then you must remember to place the compiled class into a directory structure according to its package declaration.

Hope this helps :)
 
And an explanation of how to create a JAR :)

Assuming you have compiled the file Example.java using:
Code:
javac -d c:\java\classes Example.java
Now change directory to c:\java\classes. Once there, create a small text file called main.txt containing the single line:
Code:
Main-Class: com.toolkit.Example
Now create your JAR using:
Code:
jar cvmf main.txt example.jar com
You can then run your JAR using either:
Code:
java -cp c:\java\classes\example.jar com.toolkit.Example
OR
java -jar example.jar
The second method makes use of the Main-Class property specified in the manifest file created when building the JAR.

Hope this helps :)
 
1) The package mess:

Say you have a JavaPrograms directory for your java sources. If you place all your codes in there it will be a mess sooner or later. Just like your documents directory. So you sort your codes in different directories, job, school, fun, etc. But how do you compile and run your codes? It would be nice to compile and run every code from the JavaPrograms directory, so the JVM let you do that adding a package statement to your codes. (off course it is not the only motivation/adavantage of this feature).
This way you can compile and run your codes from the JavaProgram directory:

Code:
cd C:\JavaPrograms
javac job\myCompany\myCompanyProgram.java
java job\myCompany\myCompanyProgram
Or you can use periods as the directory separator:
Code:
java job.myCompany.myCompanyProgram

2) The CLASSPATH mistery:

Now, what if I don't want to be in the C:\JavaPrograms directory to run my java classes. Well, the JVM can access an enviroment variable called CLASSPATH, which have a list of directories where it can find Java classes. So if this variable has the following list:

Code:
CLASSPATH=.;C:\JavaPrograms

And you are over the C:\Games directory and type:

Code:
java job.myCompany.myCompanyProgram

The JVM will look in the first directory of the CLASSPATH list ( . which means the current directory) for the job directory. As it can't found the job directory in
Code:
C:\Games
, it will look in the next directory from the list,
Code:
C:\JavaPrograms
where it will found the job directory. Then it will look for the
Code:
myCompany
directory and inside it for the myCompany class, which then is executed.
All directories specified in the CLASSPATH list must start from the filesystem root <absolute path> (DOS-WIN: C:\ - UNIX: /).


But, How do I set the CLASSPATH variable?

It depends on the OS. For every OS, the CLASSPATH as any other variable has a scope. It could be global or local. If you set it in a console, it will have local scope, so the commands typed in that console will see it. If you set it globally, all consoles opened in the system will see the variable. Also you can give the CLASSPATH for a single java command with the java option -classpath or -cp in any OS:

Over any directory:
Code:
java -classpath .;C:\JavaPrograms job.myCompany.myCompanyProgram


Setting the local CLASSPATH:
- For DOS, W9X-WME:
Code:
set CLASSPATH=.;C:\dir1;C:\dir2;C:\dir3
- For UNIX/LINUX systems:
(note the list separator is : and not ;)
bash shell:
Code:
export CLASSPATH=.:/dir1:/dir2:/dir3
other shells (ksh, csh, sh)
Code:
set CLASSPATH .:/dir1:/dir2:/dir3
(yes, without the '=' sign)

Setting the global CLASSPATH:
- For DOS-W9X-WME
Edit the
Code:
c:\AUTOEXEC.BAT
file and add the CLASSPATH declaration for a local variable.
- For wXP-2K:
You can set the CLASSPATH in the system properties. To set it you must open the control panel and double click System (or type the windows key <between ctrl and alt> and the pause key at the same time). There you must go to advanced and Enviroment variables. There is a list with all the enviroment variables, including the CLASSPATH. If it is not present you can create it.

- For UNIX/LINUX systems:
It depends on the shell used. basically you can set it in the
Code:
/etc/.profile
file using the declarations for local variables, also you can set it in the local .profile file of each user. Check your system documentation for information on &quot;how to set enviroment variables&quot;.


And, how do I view the CLASSPATH content?
- For DOS, W9X-WME-NT-2K:
print the content of the variable:
Code:
echo %CLASSPATH%
or print the contents of all the enviroment variables:
Code:
set

- W-NT/2K users can find more information here:
XP users can check the variable list in the System properties as explained above.

- For UNIX/LINUX systems:
print the content of the variable:
Code:
echo $CLASSPATH
or print the contents of all the enviroment variables:
bash shell:
Code:
export
other shells (ksh, csh, sh)
Code:
set

3) The JAR stuff
If you want to deploy an application, it would be nice if you dont have to create the whole directory structure to run your program. So you can create a jar file (a kind of zip file) containing the root directory of your application and include it in the CLASSPATH:
in
Code:
C:\JavaPrograms
jar xvf job.jar job

(Type
Code:
jar -h
for more options)

And then distribute the file, wich can be executed this way:
Code:
java -classpath C:\deployDirectory\job.jar job.myCompany.myCompanyProgram


The jar could be included in the CLASSPATH variable too. And you can include any jar you may need, as you can include any directory you may need.

You can find more information about jar files here:

You can find more information about the standard package names here:

You can find a detailed explanation of this whole issue here (recommended):




Well, I really hope it helps. Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top