Running java program in windows cmd. Passing string arg
Hey, this is probably a simple question, but I am having trouble running my java program from command line. I have 3 java files that I compiled and now I have 3开发者_如何转开发 class files in a directory. I want to run them and pass a string parameter to my main.
Code Sample:
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");
}
}
How I am running it:
java -classpath . Main.class testString
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: Main/class Caused by: java.lang.ClassNotFoundException:Main.class . . . . Could not find the main class: Main.class
New Error:
Exception in thread "main" java.lang.NoClassDefFoundError: dfa/class Caused by: java.lang.ClassNotFoundException:dfa.class ....Could not find the main class: dfa.Main
You shouldn't put the .class
extension when your run java.
java -classpath . Main testString
is enough if you class is in the default package.
By default, the first non-option argument is the name of the class to be invoked. A fully-qualified class name should be used.
It means that if your class is in a package you have to use java my.package.project.Main
In your case :
java -classpath . dfa.Main testString
To execute this command you must be in the parent folder of dfa directory.
Resources :
- Java documentation for Unix
- Java documentation for Windows
Try this:
java -classpath . dfa.Main testString
The name of your class must be the fully resolved class name, including the package.
精彩评论