Java unable to invoke main method

How to call java method on main class

I’m a beginner in Java and I have a very simple problem. I’m trying to finish an activity and I forgot how to call a method on the main class. I keep getting an error whenever I try ways to call the computeSum method on the main class.

Error: Error: Main method not found in class array.Array, please define the main method as: public static void main(String[] args) public class Array < public static void main(String[] args )< //Dont know what to put here to call computeSum >public int computeSum(int[] nums) < int sum = 0; for (int i=0; ireturn sum; > > 

7 Answers 7

Suppose if your class is there in com.arr package. you can specify the qualified name of the class at the time of creating object. Because Array class is already available in java.util package. It is better to create object to your Array class along with package name.

public static void main(String[] args)< com.arr.Array a1 = new com.arr.Array(); int[] numberArray = ; a1.computeSum(numberArray); > 

computeSum() it’s an instance method. we have to create Object to your class for calling the instance methods.

public class Array< public static void main(String[] args )< //Dont know what to put here to call computeSum int[] nums = ; int sum=computeSum(nums); System.out.println(sum); > public static int computeSum(int[] nums) < int sum = 0; for (int i=0; ireturn sum; > > 

You need to create an instance to invoke member method of a class.

public static void main(String[] args )< Array myArray = new Array(); int[] values = new int[] ; myArray.computeSum(values); > 

You can read about instance methods and static methods. computeSum() is an instance method, which means it would need an object of Class Array to be called, example:

public static void main(String[] args)< Array array = new Array(); int[] nums = ; array.computeSum(nums); > 

Alternatively, you could make it a static method to use it without making an object, which is not recommended, but incase you want, this is how you can do it:

public class Array< public static void main(String[] args )< int[] nums = ; int sum = computeSum(nums); > public static int computeSum(int[] nums) < int sum = 0; for (int i=0; ireturn sum; > > 

First you need to understand the difference between static classes/members and instance classes/members.

Your main method is static — as is standard for the entry method of a program — meaning it is available immediately without any binding to an instance of the Array object.

Your computeSum method is an instance method. Meaning that you need an instance of the object Array , to use it, and it will execute in that object’s context.

public static void main(String[] args) < computeSum(); // or Array.computeSum() outside of Array > public static int computeSum (int[] nums) < int sum = 0; for (int i=0; ireturn sum; > 

2) Make an instance of the Array object:

public static void main(String[] args)< Array myArray = new Array(); myArray().computeSum(); > public static int computeSum (int[] nums) < int sum = 0; for (int i=0; ireturn sum; > 

Static code — not ran in the context of an object’s instance — can not reference members that are not static, this makes sense as, how would it know what object instance of that member you are referencing ( myArray1.computeSum() ? or myArray2.computeSum() ? It doesn’t even know these two instances of the myArray object exist).

Читайте также:  example1-in-function- php MySQL examples | w3resource

Источник

«Main method not found» error when starting program? [duplicate]

I’m learning Java for my course and I’ve hit a brick wall. I’ve been tasked with developing a simple command line program. To make things easier I was given the following sample code to modify so I wouldn’t have to start from scratch.

package assignment; public class Main < private final static String[] mainMenuOpts = ; private final static String[] studentMenuOpts = ; private Menu mainMenu = new Menu("MAIN MENU",mainMenuOpts); private Menu studentMenu = new Menu("STUDENT MENU",studentMenuOpts); private DataStore data = new DataStore(); private java.io.PrintStream out = System.out; private ReadKb reader = new ReadKb(); /** Creates a new instance of Main */ public Main() < run(); >private void run() < int ret = mainMenu.display(); while(true)< switch(ret)< case 1: students();break; case 2: lecturers(); break; case 3: admin(); break; case 4: exit(); break; >ret = mainMenu.display(); > > private void students() < int ret = studentMenu.display(); while(ret != 4)< switch(ret)< case 1: addStudent();break; case 2: listStudents(); break; case 3: findStudent(); break; >ret = studentMenu.display(); > > private void lecturers() < out.println("\nLecturers not yet implemented"); >private void admin() < out.println("\nAdmin not yet implemented"); >//Student methods private void addStudent() < out.println("\n\tAdd New Student"); //prompt for details //add student to the datastore //ask if they want to enter another student - // if so call addStudent again //otherwise the method completes and the studentMenu will display again >private void listStudents() < out.println("\n\tStudent Listing"); //list all students from the datastore >private void findStudent() < out.println("\n\tFind Student"); out.print("Enter Search String: "); //reasd search text //use datastore method to get list of students that contain the search string //display matching students >// end Student methods private void exit() < data.save(); //call the datastore method that will save to file out.println("\n\nGoodbye :)"); System.exit(0); >> 
Error: Main method not found in class assignment.Main, please define the main method as: public static void main(String[] args) 

I just want to get the program running so I can understand the code better. I understand the error, but have no idea where to implement the main method in this wall of text. I’ve been experimenting for hours, but obviously as a newbie I’m completely useless. Any help would be greatly appreciated.

Читайте также:  Javascript получить из строки все числа

Источник

Main method not found even if I’ve declared it

I want to create a simple java class, with a main method, but when I compile my code, I get this error message :

Error: Main method not found in class errors.TestErrors, please define the main method as: public static void main(String[] args)

package errors; public class TestErrors < public static void main(String[] args)< System.out.println("hello"); >> 

@SotiriosDelimanolis once I had a problem using Class and it was because I carelessly created a class Class for testing purposes. Similar can happen with String and other. To prove this, try using the full name of String class, this means, change your main method to receive a java.lang.String[] args argument .

7 Answers 7

As said in my comments, looks like you’ve declared a String class among your own classes. To prove this, I’ve created a basic example:

class String < >public class CarelessMain < public static void main(String[] args) < System.out.println("won't get printed"); >public static void main(java.lang.String[] args) < System.out.println("worked"); >> 

If you execute this code, it will print «worked» in the console. If you comment the second main method, the application will throw an error with this message (similar for your environment):

Error: Main method not found in class edu.home.poc.component.CarelessMain, please define the main method as:

public static void main(String[] args) 

This usually happens if ur complete project isnotconfigured correctly or one of your class in project has still some errors in such cases IDE will prompt stating the same that project contains some error and you still proceed (ie run your class) as project has some bugs new classes will not be created and IDE will run the class which was available previously

to make sure this is ur case u can add new class in your project and try to run it and if ur getting no such class exist then there its is a perfect evidence

Источник

Why doesn’t the main method run?

My program doesn’t draw the line unfortunately. I am trying to figure out why the main method in the lineTest class doesn’t kick in? While I can make it work by changing the main method to something else, such as ‘go’ and then running that method from the ‘theGame’ class, I am intrigued as to why the main method in the lineTest class doesn’t work.

3 Answers 3

Your application has one entry point, and that entry point is the single main method that gets executed. If your entry point is the theGame class, only the main method of that class will be executed (unless you manually execute main methods of other classes).

Creating an instance of lineTest class doesn’t cause its main method to be executed.

I’ve made a start below. It looks like you might want to invest some time in following a more basic java tutorial or course to get your basic java knowledge up to speed.

Читайте также:  Поиск пути между двумя вершинами графа python

What happens in the code below is that the class theGame has a main entry for the program. The JVM will invoke the main method at the start of your program. From there, it will execute the instructions you give. So most of the times, two main methods do not make sense in a single project. Exception to this rule is if you want to have two separate application entry points two the same program (for instance a command-line application and a GUI application that use the same logic but are controlled differently).

So with the code below, you will have to specify the TheGame class as a main entry point when starting your JVM for this application.

public class TheGame < private final LineTest theBoard; public TheGame() < theBoard = new LineTest(); >public void run() < JFrame frame = new JFrame("Points"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(theBoard); frame.setSize(250, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); >/** * Main entry for the program. Called by JRE. */ public static void main(String[] args) < TheGame instance = new TheGame(); instance.run(); >> 
public class LineTest extends JPanel < public void paintComponent(Graphics g) < super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.red); g2d.drawLine(100, 100, 100, 200); >> 

Источник

Why is my VS-CODE unable to find a main method? [duplicate]

A screenshot I also tried adding a class but.. Didn’t work. second attempt

I’m not very familiar with Java, but I’m pretty sure these are methods and as such must be part of a class definition. Maybe search around the internet for «Hello World» program examples in Java. You’ll see how they are different to your code.

4 Answers 4

Your code needs following changes:

The revised code is as follows:

public class Myhello < //Myhello is a class with first alphabet in capital static void sayHello() < //sayhello is method System.out.println("hello!"); >public static void main(String[] args) < sayHello(); >> 

The thing with java is that you want every thing to be inside a class. and, remember this important bit — «the class name should match the file name.» so, that is probably the issue. i.e. — in 1, code isn’t inside any class. in second, the class name doesn’t match the file name. if you are new to java, I suggest going through w3’s java tutorial this is a text based tutorial so, you can keep your own pace or even speed run through it [do try out every thing they teach by yourself too]. I seriously recommend it since I started there and almost finished it within 1-2 weeks without any help.

try using intellij. I have had problems with getting java to run on VsCode. So, I tried intellij instead. It worked for me. So, I didnt really bother with vscode to try and fix/find the issue. Also, make sure your java install is done properly. you can find a tutorial for that on youtube easily.

Источник

Оцените статью