Common problems and their solutions for Java newbies on Microsoft Windows

Discussion in 'Java' started by Sanskruti, May 6, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India

    Compiler Problems



    1. 'javac' is not recognized as an internal or external command, operable program or batch file

    If you receive this error, it means that Windows is not able to find the compiler (javac). Here's one way to tell Windows where to find javac. Set the PATH as follows on Microsoft Windows:

    1. Click Start -> Control Panel -> System on Windows XP or Start -> Settings -> Control Panel -> System on Windows 2000.
    2. Click Advanced -> Environment Variables.
    3. Add the location of bin folder of JDK installation for PATH in User Variables and System Variables. A typical value for PATH is:

    C:\Program Files\Java\jdk<version>\bin

    Please note:

    - PATH environment variable is a series of directories separated by semi-colons (;) and is not case sensitive. Microsoft Windows looks for programs in the PATH directories in order, from left to right.
    - You should only have one bin directory for a JDK in the path at a time. Those following the first instance are ignored. If one is already present, update it to jdk<version>\bin.
    - If you are not sure where to add the path, add it to the right end of the PATH in the User Variables.
    - The new path takes effect in each new command window you open after setting the PATH variable.

    2. Class names, 'HelloWorld', are only accepted if annotation processing is explicitly requested

    If you receive this error, it means that you forgot to include the .java suffix when compiling the program. Remember, the command is javac HelloWorld.java not javac HelloWorld!

    3. Syntax Errors

    If you mistype part of a program, the compiler may issue a syntax error. The message usually displays the type of the error, the line number where the error was detected, the code on that line, and the position of the error within the code. Here's an error caused by omitting a semicolon (;) at the end of a statement:
    Code:
    testing.java:14: `;' expected.
    System.out.println("Input has " + count + " chars.")
                                                     ^
    1 error
    
    Sometimes the compiler can't guess your intent and prints a confusing error message or multiple error messages if the error cascades over several lines. For example, the following code snippet omits a semicolon (;) from the 'count++' line:
    Code:
        while (System.in.read() != -1)
            count++
        System.out.println("Input has " + count + " chars."); 
    
    When processing this code, the compiler issues two error messages:
    Code:
    testing.java:13: Invalid type expression.
            count++
                     ^
    testing.java:14: Invalid declaration.
        System.out.println("Input has " + count + " chars.");
                          ^
    2 errors
    
    The compiler issues two error messages because after it processes count++, the compiler's state indicates that it's in the middle of an expression. Without the semicolon, the compiler has no way of knowing that the statement is complete.

    If you see any compiler errors, then your program did not successfully compile, and the compiler did not create a .class file. Carefully verify the program, fix any errors that you detect, and try again.

    4. Semantic Errors

    In addition to verifying that your program is syntactically correct, the compiler checks for other basic correctness. For example, the compiler warns you each time you use a variable that has not been initialized:
    Code:
    testing.java:13: Variable count may not have been initialized.
    		count++
    		^
    testing.java:14: Variable count may not have been initialized.
    	System.out.println("Input has " + count + " chars.");
    									   ^
    2 errors
    
    Again, your program did not successfully compile, and the compiler did not create a .class file. Fix the error and try again.


    Runtime Problems



    1. Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld

    If you receive this error, java cannot find your bytecode file, HelloWorld.class.

    One of the places java tries to find your .class file is your current directory. So if your .class file is in C:\java, you should change your current directory to that. To change your directory, type the following command at the prompt and press Enter:

    cd c:\java

    The prompt should change to C:\java>. If you enter dir at the prompt, you should see your .java and .class files. Now enter java HelloWorldApp again.

    If you still have problems, you might have to change your CLASSPATH variable. To see if this is necessary, try clobbering the classpath with the following command.

    set CLASSPATH=

    Now enter java HelloWorld again. If the program works now, you'll have to change your CLASSPATH variable. The CLASSPATH variable is set in the same manner PATH variable is set as above. Note it must(CLASSPATH) point to your current directory which is 'C:\java' in this case.

    2. Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/class

    A common mistake made by beginner programmers is to try and run the java launcher on the .class file that was created by the compiler. For example, you'll get this error if you try to run your program with java HelloWorld.class instead of java HelloWorld. Remember, the argument is the name of the class that you want to use, not the filename.

    3. Exception in thread "main" java.lang.NoSuchMethodError: main

    The Java VM requires that the class you execute with it have a main method at which to begin execution of your application.
     
  2. Systemerror

    Systemerror New Member

    Joined:
    Jan 11, 2008
    Messages:
    18
    Likes Received:
    1
    Trophy Points:
    0
    Home Page:
    http://hackersparadise.synthasite.com/
    Or another way would be to go into notepad, and type: PATH=C:\Program Files\Java\jdk<version>\bin

    Save that as Autoexec.bat (or modify an existing one), log-off and log-on again, them the binary path will be set so that windows can call javac straight from the cmd prompt rather than having to type in the full path :) .
     
  3. diyarana

    diyarana New Member

    Joined:
    May 5, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    How to read a line from its line number from a file using java code?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice