3

This is my first time posting -- I found similar issues but not anything concerning this issue directly. This sounds very simple but I'm not quite sure why this is occurring. My program runs beautifully in Eclipse but not from the command line. I have a few classes within a simpletree package.

Here's BinaryTree.java:

    package simpletree;
    import java.io.*;

    public class BinaryTree implements Serializable {
       // Automatically generated UID
       private static final long serialVersionUID = -3124224583476129954L;

       BinaryTree leftNode; // left node
       BinaryTree rightNode; // right node  

       // some code
    }

    class Tree implements Serializable {
    private static final long serialVersionUID = 6591795896216994405L;
    private BinaryTree root;

    // some code    
    }

And Program1Test.java:

    package simpletree;

    public class Program1Test {
    public static void main(String[] args) {
        Tree tree = new Tree();
                // some code
    }
    }

Here's the problem: doing this from inside simpletree compiles fine:

javac BinaryTree.java Program1Test.java

When I do this:

java Program1Test

I get this:

Exception in thread "main" java.lang.NoClassDefFoundError: Program1Test (wrong n
ame: simpletree/Program1Test)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    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)
Could not find the main class: Program1Test.  Program will exit.

Any ideas? I have my classpath set correctly and I've tried running with a package (simpletree.Program1Test) and without.

peter_budo
  • 1,748
  • 4
  • 26
  • 48
Dave Brock
  • 287
  • 3
  • 5
  • 14

2 Answers2

5

you need to

java simpletree.Program1Test

from dir above simpletree

Also make required classes available using -cp

jmj
  • 237,923
  • 42
  • 401
  • 438
2
  1. Place your .class files in a subfolder named "simpletree"
  2. Use this command line:

    java simpletree.Program1Test

Mia Clarke
  • 8,134
  • 3
  • 49
  • 62
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • That's another good solution, but my classes were already in simpletree (along with the .java files) and I needed to go outside of the package to run the program. – Dave Brock May 21 '11 at 19:23