0

How I can run java program using command prompt? I already run my program using command prompt but i get this error Could not find or load main class ReadWriteTextFile

1

2 Answers 2

1

Well, there are a few conditions on running a java class:

  • First of all, the file you want to run should have a public class, with the same name as the file. For instance, Test.java would contain the Test class.

  • The public class should have a main method. This is a static method. It's arguments are the command line arguments passed. It's signature must be public static void main(String[] args).

As an example class you can call from the command line:

public class ReadWriteTextFile {
    public static void main(String[] args) {
        readWriteTextFile();
    }
    public static void readWriteTextFile() {
        // Do stuff.
    }
}

If saved in the file ReadWriteTextFile.java, you can compile and call it like this:

$ javac ReadWriteTextFile.java
$ java ReadWriteTextFile
$

Seen from the error message you get, your file is probably called ReadWriteTextFile, but you haven't got a public ReadWriteTextFile class with a main method in it.

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

4 Comments

I already have public class same name as the file. This is my public class in my program - public class ReadWriteTextFile { private static void ReadWriteTextFile()
And do you have the main method? That the method called from the command line. It should be something like public static void main(String[] args) { ReadWriteTextFile(); }
When I run $ javac ReadRiteTextFile.java I get this error - $ is not recognized as an internal or external command, operable program or batch file
I'm sorry, you shouldn't type the $. It indicates the start of a new command. You should type javac ReadWriteTextFile.java to compile, and java ReadWriteTextFile in order.
0

Check your entry point specification in your manifest as explained here:  You should set an entry like this one: Main-Class: YourPackage.ReadWriteTextFile Maybe the package path is missing? I got myself this kind of issue when launching jar files... I hope this helps.

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.