This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes  java.lang.ClassNotFoundException: when I put my code into a package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " java.lang.ClassNotFoundException: when I put my code into a package" Watch " java.lang.ClassNotFoundException: when I put my code into a package" New topic
Author

java.lang.ClassNotFoundException: when I put my code into a package

Guy Harel
Greenhorn

Joined: Oct 21, 2009
Posts: 8
I am doing java using DOS and a simple editor.

I have two files
j.java: contains a test class with Main
DotNetAppConfigFileMgr.java: contains a prototype Xml DOM wrapper for a project.

I compile using:
javac *.java -classpath C:\_Tmp\Java\Xerces-J-bin.2.9.1.zip

I run using:
java -classpath .;C:\_Tmp\Java\Xerces-J-bin.2.9.1.zip j

In DotNetAppConfigFileMgr.java, if I add the following first line: "package MyPackage", and in j.java I add "import MyPackage.*", it still compiles but does not run, and gives a class not found error. Am I missing an option when I start "java" ?

John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Guy:

Are your files in the right directory?

John.
Guy Harel
Greenhorn

Joined: Oct 21, 2009
Posts: 8
Everything is in the same directory:

dir C:\_Tmp\Java
.
..
DotNetAppConfigFileMgr.class
DotNetAppConfigFileMgr.java
DotNetAppConfigFileMgrException.class
DotNetAppConfigFileMgrException.java
j.class
j.java
MediSolutionExceptionMgr.class
MediSolutionExceptionMgr.java
Xerces-J-bin.2.9.1.zip
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Guy:

That's your problem, then. Packages map to directories, so the package this.is.a.package maps to this/is/a/package, starting on your classpath.

John.
Guy Harel
Greenhorn

Joined: Oct 21, 2009
Posts: 8
Your question turned out to be the answer. I created a sub-directory "MediSolution" and put the files theres now it runs. Thanks.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Guy:

Glad I could help.

John.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Well done, John. And welcome to JavaRanch, Guy Harel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.ClassNotFoundException: when I put my code into a package
 
Similar Threads
Classpath Problem
importing packages
Bug in JRE XML parser
installing Xerces on osx
Another CLASSPATH problem