File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes classpath/ Assertions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "classpath/ Assertions" Watch "classpath/ Assertions" New topic
Author

classpath/ Assertions

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Hi!
Can some one explain what exactly is setting the class path .
I was shown a way to run my programs , and that's how i have been doing it with out knowing why.
I save my java files in the bin folder of jdk1.5.0_15 and my class file are also stored there.

ie.

For compiling I type

C:\Program Files\java\jdk1.5.0_15\bin>javac MyTestClass.java

And then befor running a program(for the first time) I type

C:\Program Files\java\jdk1.5.0_15\bin>set CLASSPATH=

And then finally run my program
C:\Program Files\java\jdk1.5.0_15\bin>java MyTestClass

Now I am trying to enable assertion which I am unable to do so.

From what I understood the code to be used is
java -ea package.subpackage.TestClass.

I am not sure which package I am supposed to mention and whether I am supposed to run it from my present directory ie.bin

Someone please help me and tell me what's going on here..

Thanks !!


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Nabila Mohammad:
...I save my java files in the bin folder of jdk1.5.0_15 and my class file are also stored there...

This is a Bad Idea. The bin directory is for Java's "inner workings." If you add your own files there, you're headed for trouble (especially when you start cleaning things up and accidentally remove something you shouldn't). The main reason people do this is because they have not updated their PATH variable. See step 4 of Sun's installation instructions for Windows.
Originally posted by Nabila Mohammad:
...And then befor running a program(for the first time) I type

C:\Program Files\java\jdk1.5.0_15\bin>set CLASSPATH=...

This shouldn't be needed. If no classpath is specified, then Java's default is to look for classes in the current directory. So if you're compiling from the directory that stores your class files, you should be fine.
Originally posted by Nabila Mohammad:
...Now I am trying to enable assertion which I am unable to do so.

From what I understood the code to be used is
java -ea package.subpackage.TestClass.

I am not sure which package I am supposed to mention and whether I am supposed to run it from my present directory ie.bin...

Why do you say it isn't working? Is the code running at all, and just not throwing an assertion error when it should?

Note that this package configuration is a little different than what you're describing above. If the package is "package.subpackage," then the file should be in a directory called "subpackage," and "subpackage" should be in a parent directory called "package." You would then set the current directory to the parent of "package" (in your case, "bin") and compile with...

javac package/subpackage/TestClass.java

...and run with...

java -ea package.subpackage.TestClass

This should work fine with assertions using Java 5 or above. (In Java 1.4, you needed to compile using -source 1.4 for assertions to work.)
[ March 20, 2008: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I am not sure what to do.

I set the PATH variable to bin as mentioned ie.
C:\Program Files\java\jdk1.5.0_15\bin

By storing my java file in another folder(and trying to store my class file in the same folder) I cannot even compile it

Suppose I store it in "Class files" folder located in "My Documents" and try to compile MyClass.java file there,
ie.

c:\Document and settings\My Documents\Class files>javac MyCLass.java

I get the error -
javac is not an recognised as an internal or external command,operatble program or batch file

How am I supposed to compile a program elsewhere.


IF i am trying to run the program saved in the bin folder (like i have been doing )

And if I dont use the command :
set CLASSPATH=

I get an error- NoClassDefFoundError.


I got the assertion error part.
I was using packages and subpackages even when I was in the current directory.
so sorted that out.


If you can please help me out with the rest.
What does CLASSPATH specify and what does PATH specify..?

THanks for your help!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The PATH tells your system where to find Java executables, like javac and java. With the PATH set properly (to include the JDK's bin directory, which is where these executables are), you can simply type javac or java at the command prompt, and your system will use the PATH to make sense of these commands.

After updating your system class variable, did you open a new Command Prompt? Some operating systems even require a reboot for this to take effect.

The CLASSPATH tells Java where to find user-created Java class files. As I mentioned above, if there is no classpath specified, then Java's default is to look in the current directory.

So if your classes are in the current directory and Java can't find them, that indicates you have a system classpath set as an environment variable (so the default is not being used), and it does not include a dot (.) for the current directory.

See Setting the class path.

If you continue to have problems with these, it would help if you copy the complete values you have for environment variables PATH and CLASSPATH, and post them here.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Hey Marc,

Thanks alot.

It's working fine now,by setting the PATH and CLASSPATH.
I got everything right..after coming to know what they were used for and where I was going wrong.

Thanks once again.
You have been a great help!

Take care.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classpath/ Assertions