Thursday, March 27, 2014

FIRST JAVA PROGRAM, Part 2 - Common Problems for Beginners (on windows System)

In Part 1 of this series i have explained about the basic class structure, here i will let you know about the common problems that you can face while compiling and running your first java program.

When you attempt to compile your first program and you use javac command from your command prompt, then you can see following message: 
  • prob: 'javac' is not recognized as an internal or external command, operable  program or  batch file.
  • Solution : If you receive this error, Windows cannot find the compiler (javac).Here's one way to tell Windows where to find javac. Suppose you installed the JDK in C:\jdk1.7.0. At the prompt you would type the following command and press Enter:C:\jdk1.7.0\bin\javac Abc.java  If you choose this option, you'll have to precede your javac and java commands with C:\jdk1.7.0\bin\ each time you compile or run a program. To avoid this extra typing,follow my post here.
  • Prob : Class names, 'Abc', are only accepted if annotation processing is explicitly requested.
  • Solution : If you receive this error, you forgot to include the .java suffix when compiling the program. Remember, the command is javac Abc.java not javac Abc.  
While compiling your compiler can complain about wrong syntax and if your code does not follow correct syntax, you can get following message
  • Prob : 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.
        Abc.java: 3: `;' expected. System.out.println("Hello java") 
  • Solution : Follow the error message generated by compiler and check your code for syntax. 
After compilation you try to run the program and may be situation you will have a class without "main" method and you try to run it, then you will get following message.
  • Prob: java.lang.NoSuchMethodError : main Exception in thread "main".
  • Solution: to run our compiled java program our class need to have main method having signature as public static void main(String[] args){ } 
While trying to run your program there can be situation where java run-time system could not find .class file then, you will get following message
  • Prob : Exception in thread "main" java.lang.NoClassDefFoundError: Abc
  • Solution : If you receive this error, java cannot find your bytecode file, Abc.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 Abc again.

    If you still have problems, you might have to change your CLASSPATH variable. To see if this is necessary, try to set the classpath with the following command.
    set CLASSPATH=
    
    Now enter java Abc again. If the program works now, you'll have to change your CLASSPATH variable. To set this variable, click  here. The CLASSPATH variable is set in the same manner. 
     
  • Prob: could not find and load main class in Abc.class
  • Solution : 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 Abc.class instead of java Abc. Remember, the argument is the name of the class that you want to use, not the filename.

No comments:

Post a Comment