Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am new to java programming and I wrote a simple code :
Java
package test1;

public class Test1 {

	public Test1() {
		public static void test (int num1,int num2,int num3) {
			num1= 20 ;
			num2= 22 ;
			num3= num1+num2 ;
			System.out.println(num3);
		}
	}

}

and when I try to run it I get the error "Error: Could not find or load main class" in eclipse neon.

What I have tried:

I tried running this program and also hello world program with cmd and I get this error again.
I have searched alot but none of the solutions worked.
Posted
Updated 14-Oct-16 10:55am
v2
Comments
[no name] 14-Oct-16 12:19pm    
Read the error message. Then read your code. Where is method in your code located that is named "main"?

https://docs.oracle.com/javase/tutorial/getStarted/application/
Patrice T 14-Oct-16 12:22pm    
Advice: read your lessons and/or tutorials.
you have a lot of reading ahead.
Member 12793942 14-Oct-16 12:24pm    
Is my code correct?
Why doesn't it work with hello world code?
Patrice T 14-Oct-16 12:34pm    
Your code is not correct since you get an error message !

"Why doesn't it work with hello world code?"
because your code do not match the example code.
Member 12793942 14-Oct-16 12:47pm    
I copied the hello world code from internet and it didnt work .

1 solution

First of all, I noticed that you are trying to use nested functions in Java, sadly, they are not at all supported in the Java language.
Java
public Test1() {
public static void test (int num1,int num2,int num3) {

You need to terminate the constructor, and then start another function. There is a concept of lambda functions[^], but that is something else.
Another thing that I saw in your code was that you are not following the standards for main function in Java programs. If you read the standard for Java's main function[^], you will find out that the function has to be declared as,
Java
public static void main(String[] args) { }

This is the standard that your program must follow. Try the following code instead,
Java
public class Test1 {
   public Test1() {
   }

   // Add this
   public static void main(String[] args) {
      // Call your function
      test(0, 0, 0); // Because you are setting values later. 
   }

   public static void test (int num1,int num2,int num3) {
      num1= 20 ;
      num2= 22 ;
      num3= num1+num2 ;
      System.out.println(num3);
   }
}

This should work, unless your Java compiler is expecting another class to be the main class for your program. Secondly, start learning Java from here, Trail: Learning the Java Language (The Java™ Tutorials)[^], I really enjoyed learning the Java basics from here and apply them in NetBeans. NetBeans is a simple IDE for Java beginners, Eclipse can be hard sometimes.

For more, please look:
Lesson: A Closer Look at the "Hello World!" Application (The Java™ Tutorials > Getting Started)[^]
 
Share this answer
 
Comments
CPallini 14-Oct-16 16:50pm    
5.
Afzaal Ahmad Zeeshan 14-Oct-16 17:07pm    
Thank you, Carlo.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900