嘿,这可能是一个简单的问题,但我在从命令行运行我的 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");
}
}
我如何运行它:
java -classpath . Main.class testString
错误:
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
最佳答案
运行 java 时不应添加 .class
扩展名。
java -classpath 。如果您的类位于默认包中,则 Main testString
就足够了。
By default, the first non-option argument is the name of the class to be invoked. A fully-qualified class name should be used.
这意味着如果您的类位于包中,您必须使用 java my.package.project.Main
就您而言:
java -classpath . dfa.Main testString
要执行此命令,您必须位于 dfa 目录的父文件夹中。
<小时/>资源:
关于java - 在windows cmd中运行java程序。传递字符串arg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3653822/