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

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
riza
  • 51
  • 1
  • 1
  • 4
  • possible duplicate of [Java problem: Could not find main class HelloWorld](http://stackoverflow.com/questions/3005433/java-problem-could-not-find-main-class-helloworld) – McDowell Dec 24 '12 at 08:56

2 Answers2

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.

Noctua
  • 5,058
  • 1
  • 18
  • 23
  • 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() – riza Dec 24 '12 at 08:37
  • 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(); }` – Noctua Dec 24 '12 at 08:41
  • When I run $ javac ReadRiteTextFile.java I get this error - $ is not recognized as an internal or external command, operable program or batch file – riza Dec 24 '12 at 09:53
  • 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. – Noctua Dec 24 '12 at 10:02
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.

matteo rulli
  • 1,443
  • 2
  • 18
  • 30