• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

northfield Sid
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
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)

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
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!