0

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
1
  • Post the entire class, please. This will be easy to sort out. Commented Sep 6, 2010 at 19:18

2 Answers 2

5

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 :

Sign up to request clarification or add additional context in comments.

6 Comments

Rather "you shouldn't put the .class extension".
@Colin Thanks, I was no aware that I had to include the package at the front. However, for some reason I am still having the same issue. Do you know of anything else that might be wrong?
I posted the newest error message. To get this error, I inputted java -classpath . dfa.Main testString
To execute this command you must be in the parent folder of dfa directory.
..and thus not inside the dfa folder or somewhere else.
|
2

Try this:

java -classpath . dfa.Main testString

The name of your class must be the fully resolved class name, including the package.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.