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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem for running my java file

Status
Not open for further replies.

longmatch

Programmer
Nov 1, 2001
406
0
0
I downloaded a XML parser files written in Java from Wrox website. I followed the instruction in the Beginnig XML book to install JRE. The JRE works fine. But when I run the validate program, I have the error "Exception in Thread "Main" java.lang.noClassDefFounderError: Validate.
The command I used is "java wrox.Validate name2.xml". The validate is a bat file, and the command it include is

@Echo off
java -cp classes;xerces.jar wrox.Validate %1 %2 %3 %4

The name2.xml, validate.bat, and Xerces.jar are in the same directory of Wrox. I would like to know how to run my XML parser.

If need more information to solve my program, let me know.

Thanks

haijun
 
In short, only use the batch file. The syntax should be:
Code:
validate file1 file2 file3 file4
and it looks like file2-4 might be optional. The java command is setup in there already. Read on if you want to know more details.

Explaination:
The simplest syntax to run a java class is:
Code:
java ClassName

But this class is in a package (wrox). Imagine a folder on your pc with subfolders. This is essentially how packaged classes are accessed. In the root directory (the one that would be right above wrox) you would call
Code:
java wrox.Validate
just like you would use wrox/filename to access a file.

But as you notice, there is no wrox directory, just a jar file. A jar file is essentially a zip file and maintains internal directory structure. All the classes are contained in this file just as if you have them in a folder. I hope this is making sense so far :)

Now for the problem - the classpath. This is the cause of much frustration for beginning java programmers. Simply running
Code:
java wrox.Validate
will cause java to look for the subfolder wrox in the current directory. Obviously it is not there, and this is why you get the error. If this subfolder exists in another directory somewhere else on your machine, you need to tell java where to look. Hence the -cp (or -classpath) switch.
Code:
-cp .;classes
means 'look for classes in the current directory and subfolder classes'. This would make java look for the folder wrox in the current directory and in the classes subdirectory. Since, for all intensive purposes, a jar represents a directory,
Code:
-cp examp.jar
would look for wrox in the jar file.

Putting all this together the call to run Validate would look like.
Code:
java -cp classes;xerces.jar wrox.Validate ...
where '...' would be the arguments you pass in to the program (name2.xml).

FYI: %1 %2 %3 and %4 are arguments for a batch file. For example if you ran
Code:
validate test
, %1 would equal the value 'test' and the rest of the arguments would be empty. [morning] HavaTheJut
 
Dear Havathejut:
Thank you for your time and help. I will read your explanation carefully, trying to make it work.

Have a nice weekend

Haijun
 
I just tried your suggestions. It works right now. Once again, Thank you very much.

Haijun
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top