Eclipse error during compilation of a parameterized contructor code - java

When I write a simple parametrised constructor program, it compiles and runs in the command line.
However when it is executed within the Eclipse IDE, I received the following exception:
Exception in thread "main" java.lang.NoSuchMethodError:
a_constructor.Test.(II)V at
a_constructor.ParametrizedConstructor.main(ParametrizedConstructor.java:15).
Code :
//write a java program which listed the concept of parameterized constructor
class Test {
int a,b;
Test (int x, int y) {
a=x;
b=y;
System.out.println("========================");
System.out.println("value of a=" +a);
System.out.println("value of b=" +b);
System.out.println("========================");
}
}
public class ParametrizedConstructor {
public static void main(String[] args) {
Test t1=new Test(10,20);
Test t2=new Test(100,200);
Test t3=new Test(1000,2000);
}
}

The ParametrizedConstructor code is clean and doesn't have any issues.
Try:
Delete the class files which were generated using the command prompt - If you are using the same location through eclipse to compile the same file.
Make sure the Java Compiler and Java Build path was matched with the JDK versions.
Alternate Solutions:
Try placing the code in Eclipse > Java Project > Under default package and run the file.
We need to have the make sure that the compilation unit is matched to the class name ParametrizedConstructor.java (i.e.., public class)
For References - Check also the below links for better understanding:
Possible Causes of 'java.lang.NoSuchMethodError: main Exception in thread "main"'

Related

java11 allows the compilation and execution in a single line

in a file named filename.java
class filename{
public static void main(String[] a){
System.out.println("From filename main method");
}
}
public class ClassName{
public static void main(String[] a){
System.out.println("From First main method");
}
}
Observe below commands:
Command 1:
C:\javaDJ>java filename.java
From filename main method
Command 2:
C:\javaDJ>javac filename.java
filename.java:7: error: class ClassName is public, should be declared in a file named ClassName.java
public class ClassName{
^
1 error
Observation:
command 1 compiles (i assume internally ) and executes successfully.
command 2 throws compilation error.
Problem Statement :
How is java cmd able to compile the file called filename.java, when the file(filename.java) contains a public class (ClassName)which is not named 'filename.java' (the name of the file-name.) ?
To highlight a specific section from the JEP#Launch Single-File Source-Code Programs with regards to the behavior
In source-file mode, execution proceeds as follows:
The class to be executed is the first top-level class found in the
source file. It must contain a declaration of the standard public
static void main(String[]) method.
The feature which enabled you to execute Command 1 successfully was introduced in Java 11. The feature allows you to execute a Java source code file directly using the java interpreter. The source code is compiled in memory and then executed by the interpreter, without producing a .class file on disk. Check this for more information.
The error you got in Command 2 has been there since the beginning of Java.

without main class in java throws error while running [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Printing message on Console without using main() method
Can someone suggest how can a JAVA program run without writing a main method..
For eg:
System.out.println("Main not required to print this");
How can the above line be printed on console without using the public static void main(String arg[]) in the class.
Up to and including Java 6 it was possible to do this using the Static Initialization Block as was pointed out in the question Printing message on Console without using main() method. For instance using the following code:
public class Foo {
static {
System.out.println("Message");
System.exit(0);
}
}
The System.exit(0) lets the program exit before the JVM is looking for the main method, otherwise the following error will be thrown:
Exception in thread "main" java.lang.NoSuchMethodError: main
In Java 7, however, this does not work anymore, even though it compiles, the following error will appear when you try to execute it:
The program compiled successfully, but main class was not found.
Main class should contain method: public static void main (String[] args).
Here an alternative is to write your own launcher, this way you can define entry points as you want.
In the article JVM Launcher you will find the necessary information to get started:
This article explains how can we create a Java Virtual Machine
Launcher (like java.exe or javaw.exe). It explores how the Java
Virtual Machine launches a Java application. It gives you more ideas
on the JDK or JRE you are using. This launcher is very useful in
Cygwin (Linux emulator) with Java Native Interface. This article
assumes a basic understanding of JNI.
Up until JDK6, you could use a static initializer block to print the message. This way, as soon as your class is loaded the message will be printed. The trick then becomes using another program to load your class.
public class Hello {
static {
System.out.println("Hello, World!");
}
}
Of course, you can run the program as java Hello and you will see the message; however, the command will also fail with a message stating:
Exception in thread "main" java.lang.NoSuchMethodError: main
[Edit] as noted by others, you can avoid the NoSuchmethodError by simply calling System.exit(0) immediately after printing the message.
As of JDK6 onward, you no longer see the message from the static initializer block; details here.
public class X { static {
System.out.println("Main not required to print this");
System.exit(0);
}}
Run from the cmdline with java X.
Applets from what I remember do not need a main method, though I am not sure they are technically a program.

Eclipse runs the program, same does not work when ran through command prompt

I was going through some basic java concepts. I was looking into methods and trying to verify this error
"MethodSignature.java:10: error: method m1(String) is already defined in class MethodSignature
public static void m1(String s)"
The above error appears when I run from command prompt. But when running through eclipse, although it shows error, program prints the desired strings. I do not get any error as stated above in case of command prompt.
Why there is a difference in execution in Eclipse and command prompt?
As in command prompt, I am not able to run the program itself because the error should stop me. I was expecting the same in Eclipse.
Here is my simple program.
public static void m1(String s)
{
System.out.println(s);
}
public static void m1(String s)
{
System.out.println(s);
}
public static void main(String[] args)
{
m1("call one");
m1("call two");
}
You must be running a previously compiled class, you cannot declare m1 twice. However, eclipse does have its' own compiler (ecj); and it is possible to run code ignoring errors (in which case it removes the invalid code for you).

Java compile errors ISeries QShell

I have the following helloworld class
class HelloWorld {
public static void main (String args[]) {
System.out.println("Hello World");
}
}
This is in an ASCII streamfile on the IFS called helloworld.java. When I try and compile this in QSH I get the following error
javac helloworld.java
helloworld.java:2: ')' expected
public static void main (String args[]) {
¢
I can't see a missing ')' in line 2. I suspect this is a codepage error because I've also never seen ¢ as placeholder on compile output.
Any ideas ?
Personally, I would suggest edit, compile, and test, your java code on a workstation (PC or Mac) and not trying to compile on the IBM i.
It's much more efficient that way.
Once you have code that works the way you want it to on your workstation, create a deployable JAR file and move that to the IBM i for further testing.

Different results when compiling with command prompt and BlueJ

I'm just starting Java ... again.
I just made a simple program
class first
{
public static void main()
{
System.out.println("Hello!");
}
}
This runs perfectly fine in BlueJ but it gives an error during run-time when running from command prompt.
This is the error
Exception in thread "main" java.lang.NoSuchMethodError: main
It's because I didn't give String args[] in the main parameter list
Till now, I used to give it subconsciously. I know that the string array contains all the parameter values when running but then why is it running in BlueJ?
(BlueJ is a student-friendly Java editor and compiler)
Your program is valid and will compile to the same thing whether you compile from BlueJ or from the command line.
However, blueJ will let you run any static method in a class (so you can test your functions) where as the command line java command will (only) look for a special main method to run. This main method tages a String array with all the command line parameters and your program should look like this even though you don't use these command line parameters:
class first
{
public static void main(String[] args)
{
System.out.println("Hello!");
}
}

Categories

Resources