• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Interpreter looks for main method in an unexpected class

 
Greenhorn
Posts: 13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Examples are from "Thinking in Java".

import static net.mindview.util.Print.*;



Then I compile and run:



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?
 
Marshal
Posts: 72088
312
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't writefollowed byIt is eitheron its own without using javac at all (I think, Java11+ only), or it isSince you appear to have compiled the class, you will have to use what it says in line 2 of the last code block.
 
Mike Grabl-j
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. The problem is solved. But the question is still here: why it looked for the main method in Art at all?
 
Campbell Ritchie
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Grabl-j wrote:Yes. . . .

Success

why it looked for the main method in Art at all?

Afraid I don't know. Please post some more details about your Java® installation, particularly what you get from these two instructions:-...and maybe somebody else will have an idea.
 
Sheriff
Posts: 16069
267
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's because when the command issued was java ... Cartoon.java, this caused Java to go into "source-file mode".

From the documentation (https://docs.oracle.com/en/java/javase/11/tools/java.html):

In source-file mode, the effect is as though the source file is compiled into memory, and the first class found in the source file is executed. Any arguments placed after the name of the source file in the original command line are passed to the compiled class when it is executed.



If the Cartoon class declaration had been at the top instead of at the bottom, it would have run fine. Of course, since you didn't know about source-file mode, then you would have been under the wrong impression that what you did was fine. If you moved the Drawing class to the top of the file and ran the same command, it would have complained about the Drawing class instead.
 
Campbell Ritchie
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am surprised it went into source file mode after a javac command. I thought you weren't allowed both types of execution instruction together.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic