File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Strange error on running the simplest code there is. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strange error on running the simplest code there is." Watch "Strange error on running the simplest code there is." New topic
Author

Strange error on running the simplest code there is.

Pawel Nowacki
Ranch Hand

Joined: Nov 14, 2008
Posts: 67
I know it sounds ridiculous, but its true!



It compiles and run fine. Now, when I change Klasa0 to Klasa1 it compiles but when i try to run it, I get:


Exception in thread "main" java.lang.NoSuchMethodError: Klasa1.<init>(Ljava/lang/String;)V
at Test.main(Test.java:3)


The same happens when i put 2 and 3 at the end of Klasax name.
Here comes the best, when I change it to Klasa4 it compiles but when I try to run it gives:


Exception in thread "main" java.lang.InstantiationError: Klasa4
at Test.main(Test.java:3)


When i change it to Klasa5 (6, 7, 8, 9 or 0) its OK, but not for (Klasa1, Klasa2, Klasa3 and Klasa4 - gives different error)
Unfortunately (for me) i 've checked it on two other machines and it runs fine with all names.

What kind of magic may be lurking in my JVM that stops me from running this code?

My version of java is :

java version "1.6.0_06"
Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode, sharing)


And i've tried to compile it with -source 1.3, 1.4 and 1.5. The effect is the same.

Anyone have a clue?



Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4176
    
  21

Can you post your code after you make the change? It almost looks like you are changing the class name, but not the Constructor name as well.

-- Edit --

I take that back. Take a look around your classpath, it sounds like you may already have classes named Klasa1, Klasa2, Klasa3, or Klasa4 in your classpath, which may be interfering with your running.


Steve
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The most likely explanation is that your first machine is already littered with class files named "Klasa1", "Klasa2", etc, somewhere on your class path, and those classes don't include the same constructor. Use your operating system's facilities for finding files to find all files named "Klasa*.class", and delete them, then try again.


[Jess in Action][AskingGoodQuestions]
Pawel Nowacki
Ranch Hand

Joined: Nov 14, 2008
Posts: 67
OK, I expected that question
Here what im doing step by step:

1. Write and save (Test.java) this code in notepad:

class Test {
public static void main (String [] args) {
Klasa0 k = new Klasa0("a");
}
}

class Klasa0 {
private String name;

Klasa0(String name) {
this.name = name;
}
}

2. Compile:

# javac Test.java
#


3. Run:

# java Test
#


4. Open Test.java in notepad.
5. Replace all "Klasa0" to "Klasa1":



6. Compile:

# javac Test.java
#


7. Run:

# java Test
Exception in thread "main" java.lang.NoSuchMethodError: Klasa1.<init>(Ljava/lang/String;)V
at Test.main(Test.java:3)





Pawel Nowacki
Ranch Hand

Joined: Nov 14, 2008
Posts: 67
Ernest, you were right. I had Klasa1, Klasa2, Klasa3 and Klasa4 class files addressed somwhere in the middle of my CLASSPATH var.
I thought my JVM went crazy, but that was only me in the end ..
Thank you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Strange error on running the simplest code there is.