File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes running a packaged java class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "running a packaged java class" Watch "running a packaged java class" New topic
Author

running a packaged java class

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
If I compile a class using 'javac -d' and the class goes to a package, how do I run that class with the 'java' command? It doesn't seem to work in the source directory or when I go into the package directory.
Thanks.
Paul
Greg Ostravich
Ranch Hand

Joined: Jul 11, 2002
Posts: 112
Paul,
Can you please post exactly what you're typing to run your classes (your java command after you've compiled)?
It would also be helpful to see what directory you're running it from and what directory the classes or package is in.
Finally - are you trying to run it in a jar or just as the classes?
Thanks -


Greg Ostravich - SCPJ2
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
My package is 'keypad.data.jaxb'.
I compile my class in my C:\test directory like this -
javac -d DataAccess.java
It contains a main() method so to run it I've tried all four of the following from the same directory I compiled from.
java DataAccess
java -d DataAccess
java keypad.data.jaxb.DataAccess
java -cp C:\test\keypad\data\jaxb DataAccess
All of them have given me a noClassFound exception.
Thanks for your help.
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
Originally posted by Paul Keohan:
My package is 'keypad.data.jaxb'.

By this, I assume you mean that you have the statement:
package keypad.data.jaxb;
at the beginning of your java file.

I compile my class in my C:\test directory like this -
javac -d DataAccess.java

Something is missing there - there needs to be some directory after the "-d". Maybe you mean:
javac -d c:\test DataAccess.java
or
javac -d . DataAccess.java
In that case, javac is going to put your class in its package in the "-d" directoroy. That is, you will end up with:
c:\test\keypad\data\jaxb\DataAccess.class

It contains a main() method so to run it I've tried all four of the following from the same directory I compiled from.
java DataAccess
java -d DataAccess
java keypad.data.jaxb.DataAccess
java -cp C:\test\keypad\data\jaxb DataAccess

Try this:
java -classpath C:\test keypad.data.jaxb.DataAccess

The full name of a class is the package name + class name. That's what you have to give to the java command. That's the keypad.data.jaxb.DataAccess there at the end.
It's going to convert that whole thing to a directory structure plus class file, in this case keypad\data\jaxb\DataAccess.class. It looks thru classpath to find that.
So classpath has to point to the "top of the package tree". That's why your classpath has to include C:\test rather than anything else.
So when the java command puts it all together, it will load C:\test + \keypad\data\jaxb\DataAccess.class.
Hopefully you will get something good, or at lease a different exception then
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Note that some platforms get a bit confused by spaces in the classpath setting. This can often be resolved by surround the classpath setting with double quotes.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
The problem has been solved. I had another version of the class out there somehwere in a jar file that was accessible from the classpath.
Thanks for all y'alls's help.
Paul
 
 
subject: running a packaged java class