Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Interpreter looks for main method in an unexpected class

  1. #1
    Junior Member
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Interpreter looks for main method in an unexpected class

    Examples are from "Thinking in Java".

    import static net.mindview.util.Print.*;
     
    class Art {
      Art() { print("Art constructor"); }
    }
     
    class Drawing extends Art {
      Drawing() { print("Drawing constructor"); }
    }
     
    public class Cartoon extends Drawing {
      public Cartoon() { print("Cartoon constructor"); }
      public static void main(String[] args) {
        Cartoon x = new Cartoon();
      }
    }

    Then I compile and run:

    michael@michael:~/Downloads/thinking_in_java/TIJ4-code-master/examples/reusing$ javac -cp /home/michael/Downloads/thinking_in_java/TIJ4-code-master/examples Cartoon.java 
    michael@michael:~/Downloads/thinking_in_java/TIJ4-code-master/examples/reusing$ java -cp /home/michael/Downloads/thinking_in_java/TIJ4-code-master/examples:. Cartoon.java 
    error: can't find main(String[]) method in class: Art

    The question is: Why is it looking for main class in the Art class? And how to make it run the main method in Cartoon class?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Interpreter looks for main method in an unexpected class

    The java program takes the name of the class as its argument, not the name of the source file.
    java TheClassName

    Why is it looking for main class in the Art class?
    I don't know. What is the source of the java command that is being executed?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Nonverbis (April 12th, 2020)

  4. #3
    Junior Member
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Interpreter looks for main method in an unexpected class

    Quote Originally Posted by Norm View Post
    What is the source of the java command that is being executed?
    The java command is written above. If I have understood you correctly.

    --- Update ---

    Well, this works:

    michael@michael:~/Downloads/thinking_in_java/TIJ4-code-master/examples/reusing$ java -cp /home/michael/Downloads/thinking_in_java/TIJ4-code-master/examples:. Cartoon

    The problem was in .java at the end. Anyway, I solved the problem. But the question is still here to stay: why it looked for Art?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Interpreter looks for main method in an unexpected class

    But the question is still here to stay: why it looked for Art?
    Where did the java program you are using come from? How did you install it? Is it part of a package.

    I use the java command that is in the JRE I've downloaded from the Oracle site.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Nonverbis (April 12th, 2020)

  7. #5
    Junior Member
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Interpreter looks for main method in an unexpected class

    If I remember correctly, it must be Oracle JDK.

    java -version
    java version "14" 2020-03-17
    Java(TM) SE Runtime Environment (build 14+36-1461)
    Java HotSpot(TM) 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)

  8. #6
    Junior Member
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Interpreter looks for main method in an unexpected class

    Quote Originally Posted by Norm View Post
    Where did the java program you are using come from? How did you install it? Is it part of a package.

    I use the java command that is in the JRE I've downloaded from the Oracle site.
    michael@michael:~$ java -version
    java version "14" 2020-03-17
    Java(TM) SE Runtime Environment (build 14+36-1461)
    Java HotSpot(TM) 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)

    It is Oracle JDK.

Similar Threads

  1. How to the main method for this class
    By TheVolum1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 20th, 2014, 07:47 PM
  2. Splitting up main method / class
    By Scren in forum What's Wrong With My Code?
    Replies: 53
    Last Post: February 8th, 2014, 04:54 PM
  3. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  4. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  5. Main method/ class problem. I can't run any script!
    By BokBok in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2012, 05:14 PM