wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes 2 questions about classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "2 questions about classpath" Watch "2 questions about classpath" New topic

2 questions about classpath

John Cristiano

Joined: Sep 23, 2006
Posts: 4
I'm still new to Java, but I have been advised to go ahead and get used to keeping .java files in one folder, and .class files in a seperate folders. So here is the first problem, I can compile code just fine, but can't run even the simplest code. For example, MyFirstApp.java:
public class MyFirstApp {

public static void main (String[] args) {

System.out.println("I rule!!!") ;



At the terminal this the output:
john@Cheesecake:~/Java$ javac -d class src/MyFirstApp.java
john@Cheesecake:~/Java$ java class/MyFirstApp
Exception in thread "main" java.lang.NoClassDefFoundError: class/MyFirstApp (wrong name: MyFirstApp)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

The MyFirstApp.class successfully compilied and was placed in the class folder. However running the java command and pointing to the class file produced the above errors. Is there something simple I missed? I can compile and run just fine if I allow the compilier to place the .class files in the same folder as the .java files.

My second problem is that I am trying to import classes from a .zip file. The instructions I have are to add that file to my classpath so the compiler can find the classes. However, the instructions are for Windows- I run Linux(Ubuntu with a bash shell to be more specific). Thanks in advance.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38081
Why ought you to keep .java and .class files in separate folders? If you are a beginner I can't see any point in that. That might be the explanation for your first app not running; you have compiled it correctly, but moved the file away from where your JVM is. You could try "java class.MyFirstApp" instead. That might work. If not, copy the .class file to whichever folder (~/Java by the looks of it) you happen to have your shell/command prompt on at the moment. From the command line enter "dir" (Windows) or "ls" (Linux) and see what that folder contains. If it contains "MyFirstApp.class" then just enter "java MyFirstApp."

Look up packages in the Java Tutorial. You see the "default package" is discouraged, but is permitted for simple training applications. If "MyFirstApp" isn't a simple training application, then what is?

The advice you have been given comes from users of IDEs. Eclipse gives the option of separate .src and .class folders, or keeping all files in a single folder. NetBeans defaults to having four separate folders for each project, including .src and .class. I prefer the single folder approach; it allows me the option of running applications from a shell or command prompt if I wish. The shell user usually keeps all the .java files in one folder and allows the compiler to create more directories (with the -d option) as required by the package declarations in the original Java code.
One problem I can foresee for you. You seem to have a single ~/Java folder. I would suggest you create subfolders in that, to keep your programming in. It is easy enough to use a ~/Java directory when you have "MyFirstApp" in it, but when you get round to having "MyNinetyNinthApp" in it too, it will become unmanageably large.

Hope that is of some help to you.

Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77

I think, it has to do with packages. Maybe try something like that:

package class.src
public class MyFirstApp {

public static void main (String[] args) {

System.out.println("I rule!!!") ;


I did not try it, so I am not quiet sure, if it works. But now the machine knows, your classe MyFirsApp is in the package class.src. That means, src is a subfolder of the folder class.

Best Regards

SCJP 1.4
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38081
Urs Waefler's suggestion may actually make things more complicated. If you use "package class.src;" you would have to write "java class.src.MyFirstApp" at your bash prompt to get it to run.
As for the zip, you know Java has things called .jar files? "Jar" is short for "Java archive." See this part of the Java tutorial. You will find that the .jar format is the same as zip. You might be able to add the zip file with the -cp option, or you might have to unzip it, package it as a jar and then add with the -cp option. The JVM option sought to be the same on Linux as on Windows.

Anybody else able to help better?
Rangan Iyengar

Joined: Feb 23, 2007
Posts: 8

you could add the path of your class folder to the CLASSPATH variable.
or you could even give the path of the class folder when using the java command using the -cp option.

I agree. Here's the link: http://aspose.com/file-tools
subject: 2 questions about classpath
Similar Threads
Basic Question about main
Help with running my first program.
Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstApp. Please help basic issue
cannot run my java code after using Ant