• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

running a packaged java class

 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 -
 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic