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 main() in package directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running main() in package directory" Watch "Running main() in package directory" New topic
Author

Running main() in package directory

Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
I can't run a class' main() method from within the package
directory.
Here is my class definition, stored in d:\someDir\com\sergeplourde\util\Util1.java:
package com.sergeplourde.util;
public class Util1
{
public Util1()
{
System.out.println("com.sergeplourde.util");
}
public static void main(String[] args)
{
Util1 u = new Util1();
System.out.println("Done!");
}
}
This compiles ok. But when I try, from within this same directory:
java Util1
or java com.sergeplourde.util.Util1
I get the either:
Exception in thread "main" java.lang.NoClassDefFoundError: Util1 (wrong name: co
m/sergeplourde/util/Util1)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:477)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:10
9)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:249)
at java.net.URLClassLoader.access$1(URLClassLoader.java:217)
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:192)
at java.lang.ClassLoader.loadClass(ClassLoader.java:298)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:285)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:314)
or
Exception in thread "main" java.lang.NoClassDefFoundError: com/sergeplourde/util
/Util1
This is really annoying because I would expect to have this class
run without specifying anything else.
Could anyone help me?
Thanks.
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
Have you managed to compile the .java file? If not maybe it's a problem with your CLASSPATH, make sure d:\someDir\ is in your CLASSPATH and try javac com.sergeplourde.util.Util1.java from this point in the directory structure.
Hope this has helped!!


Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Thanks for your help.
Yes, I was able to compile the class.
I:
set classpath=d:\somedir
But it still does not work. By the way, why is it giving the path as: com/sergeplourde/util/Util1 ? I am using Windows NT, so
the directory delimiter should be "\".
I have the same problem at home under Windows95.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
You can associate a package name with a directory structure. In order to execute the java class in your example, you cd to
d:\someDir
Then type java com.sergeplourde.util.Util1 .
In order for java to execute a class inside a package, first the root must be from the current working directory or one of the paths listed in the classpath (if you had added d:\someDir to your class path, you could execute the program from any directory you like using the same command above). And the class file must reside in a directory that equals the package name. Java validates it has the correct class by comparing the fully qualified name that you typed in with the fully qualified name of any classes it finds with that name. If it is unable to find an exact match ....


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
V Srinivasan
Ranch Hand

Joined: Aug 16, 2000
Posts: 99
I think setting classpath like this...
set classpath=d:\somedir
...will not get visible of ...
d:\someDir\com\sergeplourde\util\Util1.java
...if I am not mistake!
Set your class path like...
set classpath=d:\somedir\*... and try
If I am wrong please correct me.
Good luck.
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
Got it! I've tried this and it works.
from somedir type - java com/sergeplourde/util/Util1
notice I use / and not \ I don't know why this is, but it works!!
Michael OConnor
Greenhorn

Joined: Jun 18, 2001
Posts: 2
I am having a very similar problem with a different package,
Maybe if you have a chance you could have a look ...
I'm using a pakage whereby i unzip it into c:\snmp-0.1 and put the c:\snmp-0.1\aka directory ( where all the .class files are) into a directory on my classpath
where i store all my classes
When i compile the snmp.java in c:\simple-snmp\snmp-0.1\Snmp\aka it compiles fine
But when i go to run it i get the following error :
The package zip is at http://www.zip.com.au/~lex/snmp/snmp-0.1.zip
If you unzip it to the c:\ the file i'm trying to run is in c:\snmp-0.1\aka\snmp\Snmp.java
the line in Snmp.java is
package aka.snmp;
Any help is *greatly appreciated,
Thanks,
Michael
---------- java ----------
java.lang.NoClassDefFoundError: Snmp (wrong name: aka/snmp/Snmp)
<stack info>
Exception in thread "main" Normal Termination
Output completed (2 sec consumed).
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Do as Angela suggested above... run the program like this...



-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Sherry Wasserman
Greenhorn

Joined: Mar 01, 2003
Posts: 2
Originally posted by Angela Poynton:
Have you managed to compile the .java file? If not maybe it's a problem with your CLASSPATH, make sure d:\someDir\ is in your CLASSPATH and try javac com.sergeplourde.util.Util1.java from this point in the directory structure.
Hope this has helped!!
Sherry Wasserman
Greenhorn

Joined: Mar 01, 2003
Posts: 2
Originally posted by Angela Poynton:
Got it! I've tried this and it works.
from somedir type - java com/sergeplourde/util/Util1
notice I use / and not \ I don't know why this is, but it works!!


Sorry for the previous blank email -- I'm new at this).
Anyway, thanks Angela for helping solve a problem which I have been struggling with for several days now. I tried all sorts of changes to the classpath settings, command line classpath, etc. etc. Never would have occurred to me to switch the directions of the slashes!
(I also found from another posting that dots instead of slashes also works!)
Thanks again!
Sherry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running main() in package directory