无法使用包从命令行运行多类程序

发布于 2024-11-09 01:18:53 字数 2134 浏览 0 评论 0原文

这是我第一次发帖——我发现了类似的问题,但没有直接与此问题相关的任何内容。这听起来很简单,但我不太确定为什么会发生这种情况。我的程序在 Eclipse 中运行得很好,但不能从命令行运行。我在 simpletree 包中有一些类。

这是 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    
    }

Program1Test.java

    package simpletree;

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

这是问题:从 simpletree 内部执行此操作可以正常编译:

javac BinaryTree.java Program1Test.java

当我这样做时:

java Program1Test

我得到这个:

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.

有什么想法吗?我的类路径设置正确,并且我尝试使用包(simpletree.Program1Test)和不使用包来运行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞清仓 2024-11-16 01:18:53

您需要

java simpletree.Program1Test

从上面的目录 simpletree

还可以使用 -cp 提供所需的类

you need to

java simpletree.Program1Test

from dir above simpletree

Also make required classes available using -cp

空城缀染半城烟沙 2024-11-16 01:18:53
  1. 将 .class 文件放在名为“simpletree”的子文件夹中
  2. 使用此命令行:

    java simpletree.Program1Test

  1. Place your .class files in a subfolder named "simpletree"
  2. Use this command line:

    java simpletree.Program1Test

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文