Common Programming Errors by Beginners in Java

8
Concepts of Java Programming Page 9 Common Errors for Beginners Consider a class named Test is saved in a file Test.java and saved in D:\ Java directory class Test { public static void main(String rk[]) { System.out.println("Error???"); } } Case 1: javac is not recognized as an internal or external command This error occurs because you have not set the environment variable in your machine. Follow the following steps: 1. Copy the path up to bin folder in jdk. (C:\Program Files\Java\jdk1.8.0_65\bin) 2. Right click on My Computer icon on desktop. Then follow the following Properties Advanced system settings Advanced Environment Variables 3. In System Variables click on New and fill the Variable name and Variable Value as shown below:

Transcript of Common Programming Errors by Beginners in Java

Page 1: Common Programming Errors by Beginners in Java

C o n c e p t s o f J a v a P r o g r a m m i n g

Page 9

Common Errors for Beginners

Consider a class named Test is saved in a file Test.java and saved in D:\ Java directory

class Test

{

public static void main(String rk[])

{

System.out.println("Error???");

}

}

Case 1: javac is not recognized as an internal or external command

This error occurs because you have not set the environment variable in your machine. Follow the following steps:

1. Copy the path up to bin folder in jdk. (C:\Program Files\Java\jdk1.8.0_65\bin)

2. Right click on My Computer icon on desktop. Then follow the following

Properties Advanced system settings Advanced Environment Variables

3. In System Variables click on New and fill the Variable name and Variable Value as shown below:

Page 2: Common Programming Errors by Beginners in Java

C o n c e p t s o f J a v a P r o g r a m m i n g

Page 10

Case 2: Annotation processing

While compiling your Java program file name must be given with .java extension.

javac Test.java

Case 3: Could not find or load main class Test.java

While executing your Java program name of the class is to be passed (without any extension) which contains the main method:

public static void main (String rk[])

NOTE: In a normal java program, if you will try to execute a class which does not contain the above mentioned main method, then you will get the same error message. As the java run time environment expects that method in the class whose name you are passing during the execution.

Page 3: Common Programming Errors by Beginners in Java

C o n c e p t s o f J a v a P r o g r a m m i n g

Page 11

Case 4: file not found: Test.java

This error occurs because the file named Test.java is not located in the current working directory. So check whether the file is saved in that location or not?

Then either change the current working directory in your command prompt or change the name of the file.

In the above case the file is saved in D:\ Java but we are working in directory C:\Users\hp

So we need to change the directory as shown below:

The same error may be seen if you are in the correct working directory but the file name is different while compiling that.

If we write any other file name instead of Test.java (e.g. javac Test1.java or javac test.java), It will result in the same error.

Page 4: Common Programming Errors by Beginners in Java

C o n c e p t s o f J a v a P r o g r a m m i n g

Page 12

Case 5: Reached end of file while parsing

This error occurs when you have missed any closing braces in your program as in this program:

class Test

{

public static void main(String rk[])

{

System.out.println("Error???");

}

Page 5: Common Programming Errors by Beginners in Java

C o n c e p t s o f J a v a P r o g r a m m i n g

Page 13

Case 6: class, interface, or enum expected

This error arises due to different reasons.

(A) When you have added some extra braces as shown in the code below:

(B) When you have used class/interface keywords incorrectly as in the code below class is written as Class. Java is case sensitive and all the keywords are in lower case.

class Test

{

public static void main(String rk[])

{

System.out.println("Error???");

}

} }

Class Test

{

public static void main(String rk[])

{

System.out.println("Error???");

}

}

Page 6: Common Programming Errors by Beginners in Java

C o n c e p t s o f J a v a P r o g r a m m i n g

Page 14

Case 7: class TestJava is public, should be declared in a file named TestJava.java

This error occurs when you have created a public class but saved in a file with some other name.

Every public class must be saved in a .java file named as the name of the class, whereas other classes can be saved with any name.

NOTE: It means that in a .java file you can define N number of classes and interfaces but at most one of them can be public and the name of the file must be same as the name of the public class.

The above classes can be saved in a single .java file but the file must be named as TestJava.java.

class Test {

public static void main(String rk[])

{

System.out.println("Error???");

}

}

public class TestJava {

void testingJava()

{

System.out.println("Inside testingJava Method...");

}

}

Page 7: Common Programming Errors by Beginners in Java

C o n c e p t s o f J a v a P r o g r a m m i n g

Page 15

Case 8: cannot find symbol

This error occurs when you are referring to a variable which does not exist and the ^ is pointing to that variable. In the above case string symbol does not exist as we have used string instead of String (String is a predefined class in java.lang package). Case 9: package does not exist

Similar to the previous case we have used system instead of System in our program. System is a predefined class in java.lang package.

Page 8: Common Programming Errors by Beginners in Java

C o n c e p t s o f J a v a P r o g r a m m i n g

Page 16

Case 10: ‘;’ expected

This error occurs because we have missed ‘;’ after the statement.