从命令行在包中运行 java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19433366/
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-08-12 17:07:32  来源:igfitidea点击:

Running java in package from command line

javacommand-linepackageclasspathjavac

提问by user2756339

I have read the previously posted questions. Some are vague and none solved my problem so I am forced to ask again.

我已经阅读了之前发布的问题。有些含糊不清,没有人解决我的问题,所以我不得不再次提问。

I have two simple classes,

我有两个简单的类,

package One;
import One.Inner.MyFrame;
public class test
{
    public static void main(String args[])
    {
        MyFrame f= new MyFrame();
    }
}

And the other class is,

另一个类是,

package One.Inner;
import java.awt.*;
import javax.swing.*;

public class MyFrame extends JFrame
{
    public MyFrame()
    {
        setPreferredSize(new Dimension(400,560));
        setVisible(true);
    }
}

I am at base folder "basic" in Windows cmd. I compile using

我在 Windows cmd 中的基本文件夹“basic”。我编译使用

basic> javac *.java -d .

A folder and subfolder is created.

创建文件夹和子文件夹。

cd One
basic\One> java test

This generates a big set of errors. Many answers directed to specify the full path which didn't work. My classes are in One so specifying One using -cp didn't work either.

这会产生大量错误。许多答案都指向指定不起作用的完整路径。我的课程在 One 中,因此使用 -cp 指定 One 也不起作用。

采纳答案by Jon Skeet

You'd run it as:

你会运行它:

java One.Test

... but from the rootdirectory (basic), notfrom the Onedirectory. You always specify the fully-qualifiedclass name.

... 但来自目录 ( basic),而不是来自One目录。您始终指定完全限定的类名。

Oh, and package names in Java should be lower-case, so it should be oneand one.inner, not Oneand One.Inner. Just a convention, but one which pretty much everyone follows.

哦,Java 中的包名应该是小写的,所以应该是oneand one.inner,而不是Oneand One.Inner。只是一个约定,但几乎每个人都遵循的约定。

回答by MattC

If the directory is:

如果目录是:

basic\One

Run java from the base directory of the package:

从包的基本目录运行 java:

basic>java One.test or basic>One.test <optional arguments>

(ideally the package would be lowercase and the class upper case):

(理想情况下,包是小写的,而类是大写的):

basic>java one.Test

If you get 'does not exist' messages, then the java command cannot find classes you referenced in your class. You can point to them with the -cp option ('.' means 'here', and you can add as many places as you like divided by ';' on Windows and ':' on Linux).

如果您收到“不存在”消息,则 java 命令无法找到您在类中引用的类。您可以使用 -cp 选项指向它们(“.”表示“此处”,您可以添加任意数量的位置,在 Windows 上用“;”分隔,在 Linux 上用“:”分隔)。

basic>java -cp . one.Test
or
basic>java -cp .;..\..\someJar.jar;c:\someDirectory\classesDirectory  one.Test

回答by javageek

while creating a class with a package if you want to run it from cmd you must created a directory with same name of package put the .class in it and then you can easily run it for example you created a class with name "one" and this class in the package with name pack ,you must run these commands

在创建带有包的类时,如果您想从 cmd 运行它,您必须创建一个与包名称相同的目录,将 .class 放入其中,然后您可以轻松运行它,例如您创建了一个名称为“one”的类和此类在名为 pack 的包中,您必须运行这些命令

1 javac one.java
after compilation created a directory with the name pack ,after that run this command
2 java pack.one
Note:
all this must be done in the current working directory and the name "one" i chose it here as file name and main class name we all know the first name used in the first command is file name and second one is main class name

1 javac one.java
编译后创建了一个名为 pack 的目录,然后运行这个命令
2 java pack.one
注意:
所有这些都必须在当前工作目录中完成,名称“one”我在这里选择它作为文件名和主类名我们都知道第一个命令中使用的第一个名称是文件名,第二个是主类名

回答by Simmant

This is because if you are declaring packagein your java file, then JAVA compiler believe you are having same folder architecture in your system. In your case Java compiler looking for One as a package and then test.class., or to be very specific just look inside your .class file you can see what path it looking for. Please have a look for below Image (I my case I use Hello and Tester)

这是因为如果您在 java 文件中声明,那么 JAVA 编译器会认为您的系统中具有相同的文件夹体系结构。在您的情况下,Java 编译器将 One 作为包查找,然后查找 test.class.,或者非常具体,只需查看您的 .class 文件,您就可以看到它寻找的路径。请查看下面的图片(我的情况是使用 Hello 和 Tester)

.class

。班级

As you can see path in image is Hello/Tester(my case example), so architecture should be like Hello->Tester.

正如你在图片中看到的路径是 Hello/Tester(我的例子),所以架构应该像 Hello->Tester。

And if you are not having same architecture and want to create same while compiling, then use javacp command.

如果您没有相同的架构并希望在编译时创建相同的架构,则使用 javacp 命令。

回答by Ziaullah

The following line of Haralan Dobrev code solves the problem.

下面这行 Haralan Dobrev 代​​码解决了这个问题。

java -cp ../ one.Test