Can't find main(String[]) method in class: TapeDeck. The main method is in the other class which runs the program
Asked Answered
C

11

7

I have two classes. When the I put class TapeDeckTestDrive first on the text editor, it runs fine. When I put the TestDrive class first, it gives the error that it can't find the main class. Why is this?

class TapeDeck {
    boolean canRecord = false;
    void playTape(){
        System.out.println("tape playing");
    }
    void recordTape(){
        System.out.println("tape recording");
    }
}

class TapeDeckcTestDrive{
    public static void main(String[] args){
        TapeDeck t = new TapeDeck();
        t.canRecord = true;
        t.playTape();

        if (t.canRecord == true) {
            t.recordTape();
        }
    }
}

ERROR ON THIS FORMAT

VS

FOLLOWING WORKS FINE:

class TapeDeckcTestDrive{
    public static void main(String[] args){
        TapeDeck t = new TapeDeck();
        t.canRecord = true;
        t.playTape();

        if (t.canRecord == true) {
            t.recordTape();
        }
    }
}

class TapeDeck {
    boolean canRecord = false;
    void playTape(){
        System.out.println("tape playing");
    }
    void recordTape(){
        System.out.println("tape recording");
    }
}
Cambrel answered 22/4, 2019 at 13:6 Comment(9)
what is your java class (file) name?Dharana
The easy way, and the best too, to solve this is to have one class per file.Engraft
@Abdul Hussain Please tell the file nameVale
it runs fine, it gives the error: what is "it". How do you execute your application?Caroylncarp
@Brother Ok so I have created two files. Both file names match the class name. However, I am getting a different error now when I run TapeDeckTestDrive class. TapeDeckTestDrive.java:3: error: cannot find symbol TapeDeck t = new TapeDeck();Cambrel
@JBNizet on the cmd terminal. "java TapeDeckTestDrive.javaCambrel
That's not the best way to execute a class. First you compile your code with javac. Then you use java and pass the fully-qualified name of the class containing the main method.Caroylncarp
@JBNizet ok thanks. I learned that but for whatever reason forgot to implement that. I am getting the following error now: Exception in thread "main" java.lang.IllegalAccessError: failed to access class TapeDeck from class TapeDeckTestDrive (TapeDeck is in unnamed module of loader 'app'; TapeDeckTestDrive is in unnamed module of loader com.sun.tools.javac.launcher.Main$MemoryClassLoader @18bf3d14) at TapeDeckTestDrive.main(TapeDeckTestDrive.java:3) I will continue figuring this out on my own but help is appreciated.Cambrel
@Brother That is completely incorrect. Don't post misinformation or guesswork here. Java looks for the main method inside the class you specify.Cella
C
19

After you compile the code using the command:

javac fileName.java

Run the java .class file by only specifying fileName without the .java extension

java fileName

if you use fileName.java it won't run the specific .class file; it will try to interpret the .java file. if you want to interpret a .java file then parent class must contain the main(String[]) method.

Countdown answered 23/2, 2020 at 11:17 Comment(0)
C
5

First, You have to compile the File by using javac. Then, You have to Run the file.

Classname where main is written.

javac filename.java
java classname
Corenecoreopsis answered 5/1, 2020 at 2:13 Comment(0)
A
3

You Can Run the java program in two ways.

  1. Directly run the java program by

      java example_program.java
    

    In this type compilation and Execution happens at runtime. That is Byte codes is generated and executed immediately(works as a interpreter) So,You must use the superclass(Containing the main method) at first followed by other compound classes.

Note: No .class file will generate. That means, it will generate byte code internally and will execute. Programmer's cannot view the class file.

  1. In Second type, First, you should compile,

     javac example_program.java 
    

It will generate the example_program.class . Then, Execute the class file using,

     java example_program

Here, the order of writing classes doesn't impact. you can write the classes in any order. it will work fine.

Anesthesiology answered 27/1, 2020 at 5:34 Comment(0)
B
1

I got your problem.

First of all, check your classpath that you have set in Environment Variables

Follow the following steps:

  • ***Step 1: *** Right Click on This PC --> Advanced system settings --> Environment Variables

  • ***Step 2: *** Edit the variable classpath and add a new path or edit your old path that you have set. The path should be: C:\Program Files\Java_Home\jdk..\lib;.; Note: The "." is must after a semicolon (;).

  • ***Step 3: *** Close the CMD and open it again.

  • ***Step 4: *** Now compile your using javac command: javac FileName.java

  • ***Step 5: *** Run your code using java command: java ClassName

And there you go...

Bron answered 3/12, 2022 at 7:24 Comment(0)
C
0

I split it into two files and added public to the classes/methods as well as the boolean. Now the code runs.

Cambrel answered 23/4, 2019 at 13:49 Comment(1)
Please respond to comments in comments, or by editing the question. Answers should be answers.Racemose
W
0

In some JDK's , JVM looks after the entry point function first due to which it need to be written first then the rest of the code. As main function is our entry point function it must be written first.

Whipstall answered 17/9, 2021 at 4:41 Comment(0)
W
0
Steps 1. 
--You have to compile the File by using javac. Then, You have to Run the file.

--Classname where main is written.

-- javac filename.java
-- java classname


It causing error due to:-
class TapeDeck {
    boolean canRecord = false;
    void playTape(){
        System.out.println("tape playing");
    }
    void recordTape(){
        System.out.println("tape recording");
    }
}

class TapeDeckcTestDrive{
    public static void main(String[] args){
        TapeDeck t = new TapeDeck();
        t.canRecord = true;
        t.playTape();

        if (t.canRecord == true) {
            t.recordTape();
        }
    }
}

--Your tapedeck class doesn't main (String[]).
Wellman answered 29/5, 2022 at 19:44 Comment(0)
H
0

Try to put "public static void main" class first and then other methods in your code. It will definitely work.

Heedful answered 10/2, 2023 at 13:34 Comment(0)
S
0

I was facing the same problem. Then I realised I wrote the wrong spelling instead of main I wrote mian.

So just check with spelling mistakes.

Before

public static void mian(String args[]){
      int a[][] ={{3,3},{5,-1},{-2,4}};
      int k=2;
    
      nearestCar(a,k);
}

After

public static void main(String args[]){
      int a[][] ={{3,3},{5,-1},{-2,4}};
      int k=2;

      nearestCar(a,k);
}
Strongroom answered 1/5, 2023 at 7:17 Comment(0)
C
0

open your myClass.java directory

open cmd there

execute

javac myClass.java

then

java myClass
Cataract answered 9/3, 2024 at 17:12 Comment(0)
B
-1

I have faced the same problem. I used to run the file directly before compiling. Since you have two class files, it reads the first class and checks for main method. If it's present then it will run or else it will thrown an error

Can't find main(String[]) method in class

To avoid this sort of error, always compile the java file usng (javac fileName.java) and then run the (java fileName).

Bunch answered 22/2, 2024 at 5:30 Comment(1)
Pardon me, but I don't see what your answer adds to the other [ten] answers. Can you tell me?Demoralize

© 2022 - 2025 — McMap. All rights reserved.