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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6082107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:14:56  来源:igfitidea点击:

Can't run multiple-class program from command line using packages

javacommand-lineruntime

提问by Dave Brock

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 simpletreepackage.

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

Here's BinaryTree.java:

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

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 simpletreecompiles fine:

问题是:从simpletree内部执行此操作可以很好地编译:

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
java simpletree.Program1Test
0(Unknown Source) at java.net.URLClassLoader.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.

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

回答by Jigar Joshi

you need to

你需要

##代码##

from dir above simpletree

从上面的目录 simpletree

Also make required classes available using -cp

还可以使用 -cp

回答by Dark Falcon

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

    java simpletree.Program1Test

  1. 将您的 .class 文件放在名为“simpletree”的子文件夹中
  2. 使用这个命令行:

    java simpletree.Program1Test