• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help to execute a program  RSS feed

 
Ruslan Salimovich
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please help me to execute the following code:



The problem is the main() method is located in non-public class. So I cannot understand how to launch it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same way you normally would. It doesn't matter that A is package private, or that it shares the source file with Z.
 
Tim Moores
Saloon Keeper
Posts: 4035
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There can only be a single top-level public class in each source file. If you want to run A, then you have to make A a public class (which means moving it into its own source file).
 
Ruslan Salimovich
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to Run it in Eclipse, but it says:

"Error: Main method not found in class first.Z, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application"
 
Tim Moores
Saloon Keeper
Posts: 4035
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right: if you want to execute class Z, then that's where the main method needs to be. That there is some other class A that does have a main method is irrelevant to Z.
 
Ruslan Salimovich
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:There can only be a single top-level public class in each source file. If you want to run A, then you have to make A a public class (which means moving it into its own source file).

Yes, I understand this, but I just want to solve the followint task:

In Blips.java, copy the file and rename it to BlipCheck.java and rename the class Blip2 to BlipCheck (making it public and removing the public scope from the class Blips in the process). Remove the //! marks in the file and execute the program, including the offending lines. Next, comment out the default constructor for BlipCheck. Run it and explain why it works. Note that after compiling, you must execute the program with "Java Blips" because the main( ) method is still in the class Blips.

The code of Blips.java is:



So author doesn't say anything about moving class BlipCheck in new file.
 
Ruslan Salimovich
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so the code should be the following:

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't need to be in a different file and it doesn't need to be public.

After making the changes and compiling the classes, you can just run the application with 'java ioEx.Blips'.
 
Ruslan Salimovich
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:It doesn't need to be in a different file and it doesn't need to be public.

After making the changes and compiling the classes, you can just run the application with 'java ioEx.Blips'.

Thank you, it'sclear now.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
run the application with 'java ioEx.Blips'

For that to work, the classpath must include the folder that contains the ioEx folder.
Either change directories so that the current folder contains the ioEx folder
or use the java command's -cp option to point to the folder with the ioEx folder.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!