This discussion is archived
13 Replies Latest reply: Oct 30, 2009 10:03 AM by 843810 RSS

cannot compile java Programs

843810 Newbie
Currently Being Moderated
hi everyone,
when i tried to compile a java Program i had created.I got the following Error.
Error occoured during initialization of VM java/lang/NoClassDefFoundError: java/lang/Object

I reinstalled the entire thing from the system and when i tried to compile the java Program again it gives like a 1000 errors even on the programs i know that work fine and even the very basic 5 or 10 line Programs.Can someone tell me what is happening??
  • 1. Re: cannot compile java Programs
    EJP Guru
    Currently Being Moderated
    What do java -version and javac -J-version print? and what operating system are you running on?
  • 2. Re: cannot compile java Programs
    843810 Newbie
    Currently Being Moderated
    Its the latest version of java with windows XP service pack 3
  • 3. Re: cannot compile java Programs
    843810 Newbie
    Currently Being Moderated
    Having Deleted a file in the folder that i was executing the javac seem to have fixed the problem.It was called String.java
    and the content of the file were as mentioned under:Any help on why it happened or what the file was doing would be really appreciated.

    The content of the file:-

    ""Java String Examples

    The String class implements immutable character strings, which are read-only once the string object has been created and initialized. All string literals in Java programs, are implemented as instances of String class.

    The easiest way to create a Java String object is using a string literal:

    1.
    String str1 = "I can't be changed once created!";


    A Java string literal is a reference to a String object. Since a String literal is a reference, it can be manipulated like any other String reference. i.e. it can be used to invoke methods of String class.

    For example,

    1.
    int myLenght = "Hello world".length();


    The Java language provides special support for the string concatenation operator (+), which has been overloaded for Java Strings objects. String concatenation is implemented through the StringBuffer class and its append method.

    For example,

    1.
    String finalString = "Hello" + "World";


    Would be executed as

    1.
    String finalString = new StringBuffer().append("Hello").append("World").toString();


    The Java compiler optimizes handling of string literals. Only one String object is shared by all strings having same character sequence. Such strings are said to be interned, meaning that they share a unique String object. The Java String class maintains a private pool where such strings are interned.

    For example,

    1.
    String str1="Hello";
    2.
    String str2="Hello";
    3.
    If(str1 == str2)
    4.
    System.out.println("Equal");


    Would print Equal when executed.

    Since the Java String objects are immutable, any operation performed on one String reference will never have any effect on other references denoting the same object.

    String Constructors
    String class provides various types of constructors to create String objects. Some of them are,

    String()
    Creates a new String object whose content is empty i.e. "".

    String(String s)
    Creates a new String object whose content is same as the String object passed as an argument.

    Note: Invoking String constructor creates a new string object, means it does not intern the String. Interned String object reference can be obtained by using intern() method of the String class.

    String also provides constructors that take byte and char array as an argument and returns String object.

    String equality - Compare Java String
    String class overrides the equals() method of the Object class. It compares the content of the two string object and returns the boolean value accordingly.

    For example,

    1.
    String str1="Hello";
    2.
    String str2="Hello";
    3.
    String str3=new String("Hello") //Using constructor.
    4.

    5.
    If(str1 == str2)
    6.
    System.out.println("Equal 1");
    7.
    Else
    8.
    System.out.println("Not Equal 1");
    9.

    10.
    If(str1 == str3)
    11.
    System.out.println("Equal 2");
    12.
    Else
    13.
    System.out.println("I am constructed using constructor, hence not interned");
    14.

    15.
    If( str1.equals(str3) )
    16.
    System.out.println("Equal 3");
    17.
    Else
    18.
    System.out.println("Not Equal 3");


    The output would be,
    Equal 1
    Not Equal 2
    Equal 3

    Note that == compares the references not the actual contents of the String object; Where as equals method compares actual contents of two String objects.

    String class also provides another method equalsIgnoreCase() which ignores the case of contents while comparing.

    Apart from these methods String class also provides compareTo methods.

    int compareTo(String str2)
    This method compares two Strings and returns an int value. It returns
    - value 0, if this string is equal to the string argument
    - a value less than 0, if this string is less than the string argument
    - a value greater than 0, if this string is greater than the string argument

    int compareTo(Object object)
    This method behaves exactly like the first method if the argument object is actually a String object; otherwise, it throws a ClassCastException."""

    Edited by: Gold_y on Oct 30, 2009 4:59 AM
  • 4. Re: cannot compile java Programs
    EJP Guru
    Currently Being Moderated
    Its the latest version of java with windows XP service pack 3
    That's not what I asked. I asked you for a specific output for a specific reason.
  • 5. Re: cannot compile java Programs
    843810 Newbie
    Currently Being Moderated
    Gold_y wrote:
    Having Deleted a file in the folder that i was executing the javac seem to have fixed the problem.It was called String.java
    Doh.
    Any help on why it happened or what the file was doing
    You put it there.
  • 6. Re: cannot compile java Programs
    843810 Newbie
    Currently Being Moderated
    what i wanted to know is what is wrong with the file??Why was the compiler acting up its just a normal file i created when i was looking for help material from internet for advancing my java Programming skills by copying the content of a webpage and compiler was working fine when i created it it just started acting up the next day.Also it was working just fine when i checked it in other folders which is why i came to the decision that it was some file that was causing the Problem and located the file by trial and error method removing 1 file at a time.

    Edited by: Gold_y on Oct 30, 2009 10:34 PM

    Edited by: Gold_y on Oct 31, 2009 1:29 AM
  • 7. Re: cannot compile java Programs
    843810 Newbie
    Currently Being Moderated
    it says

    Java version "1.6.0_16"
    java(TM) SE Runtime environment (build 1.6.0_16-b01)
    Java HotSpot(TM) Client VM (Build 14,2-b01, mixed mode, sharing)
  • 8. Re: cannot compile java Programs
    843810 Newbie
    Currently Being Moderated
    Because when javac processes your
    public static void main(String[] arg)
    it tries to compile that String.java file instead of using java.lang.String
  • 9. Re: cannot compile java Programs
    843810 Newbie
    Currently Being Moderated
    i may have agreed but it was throwing about 50 absolutely same errors every time i complied any program as long as they were in the same folder as that file even if it was Hello World program which i have been using ever since i started learning the ABC of java.I mean it was like the compiler had a urge to compile the Program like iron draws to a magnet.
  • 10. Re: cannot compile java Programs
    843810 Newbie
    Currently Being Moderated
    Gold_y wrote:
    i may have agreed but it was throwing about 50 absolutely same errors every time i complied any program as long as they were in the same folder as that file even if it was Hello World program which i have been using ever since i started learning the ABC of java. I mean it was like the compiler had a urge to compile the Program like iron draws to a magnet.
    Probably every one of them was using "String".
  • 11. Re: cannot compile java Programs
    843810 Newbie
    Currently Being Moderated
    why should it even consider that when i give a command
    " javac HelloWorldApp.java"

    and the content of file is as under:-

    class HelloWorldApp {
    public static void main(String[] args) {
    System.out.println("Hello World!");
    }
    }
  • 12. Re: cannot compile java Programs
    EJP Guru
    Currently Being Moderated
    Because your code used a class called String and you had what appeared to be the source code of a Java String class, so it tried to compile that and went bananas. So don't do that: don't put things into .java files that aren't Java, and don't use names of standard classes for your own classes or files.
  • 13. Re: cannot compile java Programs
    843810 Newbie
    Currently Being Moderated
    oh well thanks for helping i get it.i have created a untitled java file that i copy and paste Programs to and kept it java type cos it keeps the renaming part a lil short.But i'll take care from now on.Thanks a million.