在windows cmd中运行java程序.传递字符串arg

PFr*_*ise 0 java

嘿,这可能是一个简单的问题,但我无法从命令行运行我的java程序.我编译了3个java文件,现在我在目录中有3个类文件.我想运行它们并将一个字符串参数传递给我的main.

代码示例:

package dfa;
public class Main {

public static void main(String[] args) {

    DFA myDFA = new DFA();

    run(myDFA, args);
}

public static void run(DFA myDFA, String[] args)
{
    String test = args[0];
    if(myDFA.accept(test))
        System.out.println("yes");
    else
        System.out.println("no");
}
}
Run Code Online (Sandbox Code Playgroud)

我是怎么运行它的:

java -classpath . Main.class testString
Run Code Online (Sandbox Code Playgroud)

错误:

Exception in thread "main" java.lang.NoClassDefFoundError: Main/class
Caused by: java.lang.ClassNotFoundException:Main.class
.
.
.
.
Could not find the main class: Main.class

新错误:

Exception in thread "main" java.lang.NoClassDefFoundError: dfa/class
Caused by: java.lang.ClassNotFoundException:dfa.class
....Could not find the main class: dfa.Main

Col*_*ert 5

.class运行java时不应该放置扩展名.

java -classpath . Main testString 如果你的类在默认包中就足够了.


默认情况下,第一个非选项参数是要调用的类的名称.一个完全合格的类名应该使用.

这意味着如果您的班级在一个包中,您必须使用 java my.package.project.Main

在你的情况下:

java -classpath . dfa.Main testString
Run Code Online (Sandbox Code Playgroud)

要执行此命令,您必须位于dfa目录的父文件夹中.


资源: