Getting strange java.lang.NoSuchMethodError error - java

I have an interface called Namable
public interface Namable { public String getName(); public void setName(String name); };
Several classes will implement this.
When I make code like this:
Namable foo = new X(); foo.getName();
(X is an implementing class)
I get the error:
java.lang.NoSuchMethodError: Namable.getName()Ljava/lang/String;
This is very strange to me and I'm wondering why this is happening, any thoughts?
Compilable example:
Namable class:
public interface Namable
{
public String getName();
}
Extending class X:
public class X extends java.util.ArrayList implements Namable
{
private String name;
public X()
{
this.name = "bar";
}
public String getName()
{
return name;
}
}
Testing class:
public class Test
{
public static void main()
{
Namable foo = new X();
foo.getName();
}
}
After running this, I get the same error. I am using BlueJ

I have run an example with the classes you have given here and it works as expected once the main method is changed to:
public static void main(String[] args)
However, as you are using BlueJ this may not be a problem (I remember BlueJ using a special mechanism to run classes).
Do you have another class Nameable in your classpath? Check the package import and make sure it is the interface you have defined. It appears the error is with your environment and not with the code.

You need to call
foo.getName()
not
X.getName()
You are trying to call a class (static) method that doesn't exist.
And it should be Namable foo = new X(); - your example code shouldn't even compile as shown.
Can you provide a SSCCE ?
Updated following corrections and SSCCE: Just a wild guess now, but does BlueJ define a Namable already, or do you have a Namable in another package that might be imported instead? What happens if you rename Namable to Namable2 throughout, in the example above?

As stated by DNA your post would be better with a SSCCE
My guess would be that maybe you could have missed a "static" keyword in main function
public static void main(String args[])
not
public void main(String args[])
Could be something a classpath problem too... Or a file name and class mismatch. Or something else.

When I ran your code snippet, the error shown is
Error: Main method not found in class Test, please define the main
method as: public static void main(String[] args)
Change your main method to
public static void main(String[] args)
I am not getting
java.lang.NoSuchMethodError: Namable.getName()Ljava/lang/String;
with your given code snippet.

Related

why the main method is printing nothing in the sub class?

"mysecondclass" which is a sub class of the super class "myfirstclass"
should inherit all the properties of the super class "myfirstclass"
and it's expected to output the value of x but instead it prints nothing
package myfirstproject;
public class myfirstclass {
protected int x = 10;
//getter
public void getX() {
System.out.println(x);
}
public static void main(String[] args) {
/* it asked me to define this main method here although it's useless in this situation
*/
}
}
class mysecondclass extends myfirstclass {
public static void main(String[] args) {
mysecondclass mysecondobject = new mysecondclass();
mysecondobject.getX(); //prints nothing
}
}
i am really newbie in java and i am actually still learning it's basics
also i do not know why it always asks me to add the main method in the super class although i have already declared one in the sub class (i heard one main method is enough for one java file)
any help is really appreciated , thanks in advance
You are not understanding the function
public static void main(String[] args)
In Java Docs :
The public static void keywords mean the Java 1 virtual machine (JVM)
interpreter can call the program's main method to start the program
(public) without creating an instance of the class (static), and the
program does not return data to the Java VM interpreter (void) when it
ends.
You have two
public static void main(String[] args)
In two different classes! You are running or compiling the one that uses myfirstclass So for this type of example or test is a good practice to have only one Java class Main
public class Main {
public static void main(String[] args) {
// Call Your classes
}
}
Be more clean and organized in your code!
And one other thing. For running by Command Line use this type of commands
java MyApp arg1 arg2
When in doubt always read the Docs!
You have 2 ways to make your code running as you wish
1) create 2 java file. one is myfirstclass.java the second is mysecondclass.java. each hold their main class. Thus you can kick off any main method as you wish.
2) just keep these 2 class in the same java file. then when you running it, it will only run the main method in default in the public class.
Just change the public keyword to the second class. If you are using an online compiler, they usually look for the main method inside the public class.
package myfirstproject;
class myfirstclass {
protected int x = 10;
//getter
public void getX() {
System.out.println(x);
}
}
public class mysecondclass extends myfirstclass {
public static void main(String[] args) {
mysecondclass mysecondobject = new mysecondclass();
mysecondobject.getX(); //printx X
}
}

Program will give an error if method not marked public ( all classes are in the same file)

I am trying to create an Anonymous class during which I came across following problem. In the following code when I change display method access modifier to default it gives an error but when I change it to public it works fine. Could you explain it to me why this happens.AFAIK public and default are work in similar as long as all classes are in same package. Please correct me if I am wrong.
//from file : Skg.java
package sandeep2;
class Skg1
{
public void display()
{
System.out.println("sandeep here");
}
}
class Skg2 {
public void say()
{
System.out.println("Skg2");
}
Skg1 obj = new Skg1()
{
**public void display()** //wont work if this is not public ????????????
{
System.out.println("I am ANONymous");
}
};
}
public class Skg {
public static void main(String[] args)
{
Skg2 x = new Skg2();
x.obj.display();
}
}
Class Skg2 attempts to create an instance of an anonymous inner class as a subclass of class Skg1. That anonymous inner class overrides Skg1.display(), which is public. You cannot override a method to reduce its visibility. Java does not permit it, and it would violate the substitution principle if you could do it.

Strange exception, JDK 6.0.29 bug?

Was experimenting with interface inheritance and found this:
interface String{}
interface Object{}
interface Exception{}
interface Integer extends String, Object, Exception{}
public class Test implements Integer{
public static void main(String[] args) {
System.out.println("Test");
}
}
it's compiling, but throws "main" java.lang.NoSuchMethodError: main at runtime?
That's because main method signature should be:
public static void main(java.lang.String[] args) {
if you write
public static void main(String[] args) {
it gets an array of your String interfaces, so the signature is different
If you commend out your inadvisable String interface, the Test class tries to extend java.lang.String, which is a final class. You can't extend it. And asking an interface like your Integer to extend a class instead of another interface should draw a compiler error.
I see nothing educational about this experiment. None of it should ever find its way into real code.

Main method in a static inner class.?

I've learnt that the only public class in a Java file must also have the main method. However, below you can see the main method inside an inner class instead?
What is the rule with regard to the main method definition in a source file?
public class TestBed {
public TestBed() {
System.out.println("Test bed c'tor");
}
#SuppressWarnings("unused")
private static class Tester {
public static void main(String[] args) {
TestBed tb = new TestBed();
tb.f();
}
}
void f() {
System.out.println("TestBed::f()");
}
}
If you want to start a class with java (the Java launcher: java test.MyClass) then this class must have a main method with the well known signature.
You can have a main method with the same signature anywhere you want. But don't expect that the launcher will find it.
P.S. The name of the language is Java, not JAVA.
There is a minor detail:
You may do this:
package test;
public class Test {
/**
* #param args the command line arguments
*/
static public class A {
public static void main(String[] args) {
System.err.println("hi");
}
}
}
java test.Test$A
but this is non standard ...
Any class that can have a static method can have a public static void main(String[] args).
This includes:
top-level classes (whether public or not), e.g.
public class Foo {
public static void main(String[] args) {
System.out.println("Hello");
}
}
and inner static classes (whether public or not) (like your example).
It does not include:
anonymous classes
inner non-static classes
So both of these are illegal:
public class Foo {
private Object bar = new Object() {
public static void main(String[] args) {
System.out.println("Hello");
}
};
}
public class Foo {
private class Bar {
public static void main(String[] args) {
System.out.println("Hello");
}
};
}
Every Java application must have a main method. It’s the starting point for the execution of the code in the application. Its method signature is:
public static void main(String[] args)
A static inner class is a class that is defined inside of a different class's definition and marked as being static.
For example, if the outer class is named TestBed, then an inner class of TestBed, which is named Tester, would be compiled into TestBed$Tester.class. The separation of .class files means that you can keep the supplemental, nested code tightly coupled to the primary, outer class.
They are in the same source file, and the inner class is actually inside the outer class. All that and you don't have to pay any sort of deployment or run time cost.
By using static inner classes, you can add additional support functionality to your systems for capabilities such as testing, while incurring no penalties in normal, production deployment.
To execute the main() method of that TestBed.Tester class,
% java TestBed$Tester
This is interesting as the code will compile and run in Eclipse, but will just compile from using commmand line. When you run from eclipse it will find the static main method from within the inner class and run it.
But when running java TestBed from the command line you will get error - Exception in thread "main" java.lang.NoSuchMethodError: main which is a valid error as you have not defined your main method in main class.
Why would you want to define your main method in an inner class? Your main method should be defined in public class, this is not a rule but common practice.
In below code I've moved the main method into outer class which works both in Eclipse & command line :
public class TestBed {
public TestBed() {
System.out.println("Test bed c'tor");
}
#SuppressWarnings("unused")
private static class Tester {
}
public static void main(String[] args) {
TestBed tb = new TestBed();
tb.f();
}
void f() {
System.out.println("TestBed::f()");
}
}

Doubt based on program in SCJP(EXAM 310-065)

class Top{
public Top(String s){System.out.print("B");}
}
public class Bottom2 extends Top{
public Bottom2(String s){System.out.print("D");}
public static void main(String args[]){
new Bottom2("C");
System.out.println(" ");
} }
In the above program, I guessed the output must be BD, but in the book they said the compilation fails. Can anyone explain this?
The derived class Bottom2 is required to call the base class constructor using super, otherwise you'll get a compile error. For example, if you do this, it will compile:
public Bottom2(String s) { super(s); System.out.print("D"); }
See the section on Subclass Constructors.
When you have public Top(String s) then java doesn't create the default constructor with no arguments then when you write the child class, the constructor look for the default constructor (because you are not calling explictly)... then the compilations fails.

Categories

Resources