my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Gives me error when included in the same folder but works when outside folder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Gives me error when included in the same folder but works when outside folder" Watch "Gives me error when included in the same folder but works when outside folder" New topic
Author

Gives me error when included in the same folder but works when outside folder

northfield Sid
Ranch Hand

Joined: Aug 08, 2002
Posts: 106
I have a class FtpExample that calls a bean.
However for some reason when I package this class
as part of the "ftp" folder and I run the program it always gives me errors. Please note in the example FtpExample is inside a folder called "ftp".
Below is error:
C:\java-coding\ftpbean\FTP>java FtpExample
Exception in thread "main" java.lang.NoClassDefFoundError: FtpExample (wrong nam
e: ftp/FtpExample)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:509)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:246)
at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:262)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:322)
C:\java-coding\ftpbean-package-Example-inside\FTP>

It is strange the error is not given when I run the FtpExample class outside of the "ftp" package.
and thus instead of
package ftp;
I have statement
import ftp.*;
I want to use the "FtpExample.java" class inside the ftp package.
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
When you said this:
java FtpExample
you asked java to run a class named FtpExample.
So it looked thru its classpath and found a file named FtpExample.class. But when it loaded that file, it found a class named "ftp.FtpExample" instead.
I assume that you have the C:\java-coding\ftpbean\FTP directory (or ".") in your classpath.
The pakage name is part of the classname, and must be specified on the command line, as:
java ftp.FtpExample
You will also need to arrange your CLASSPATH to include the C:\java-coding\ftpbean directory. It probably is there since it seems to be able to find other things in that package when you run the version of FtpExample with no package.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Gives me error when included in the same folder but works when outside folder