How to Compile and Run Java Code without any error?

0
210 views
How to run & compile java code from command line without any error
How to run & compile java code from command line without any error

If you are new Java learner and want to learn how to compile your Java code & run your Java program or facing any issue with the compilation or running your Java program, then you came at the right place. Today I am going discuss the steps which you need to follow for writing your first program and their execution and also going to discuss error/exceptions & warning you may face.

First of all let us see what are the basic steps you need to follow for your program to run using command prompt (cmd), terminal or any CLI application irrespective to any OS (Window, Linux, Solaris, Mac, etc). There are two types of package (Package is a collection of Java class inside a directory or folder) i.e. Default package and user defined package. Let’s see how we can write, compile & run our java code in each types:

1. Default Package:

 

Default Package is nothing then a directory where we create a Java file & write Java syntax into it. Lets create one file in your present directory (in our example “D:workspacejavaacpsrc” is the current directory on file system) and named it as “HelloWorld.java” and now write below syntax into it and save it.

 

Now follow these steps to compile it first and run it from command line interface:

  1. Make sure your class name & file name should match as Java is case sensitive language and must be saved with “.java” extension as “HelloWorld.java” in our example.
  2.  Make sure your current working directory is the directory where your java file exist, in our case it should be at “D:workspacejavaacpsrc”. Now use javac tool which comes along-with the JDK(Java Development Kit) to compile your Java code to the byte code, which will be used by JVM(Java Virtual Machine) to execute the program. To use this tool you simply type javac and then your Java file name with extension as “javac HelloWorld.java”, this will generate a byte code i.e. nothing else then a .class file with the same name as your class name if its successfully compiled. You can cross verify it in your file system for a file “HelloWorld.class”.
  3.  Now you can execute your program by using java tool on CLI. You need to provide your class name as a argument to the java tool as “java HelloWorld”, If everything fine it will produce the output in our case it will print a statement on CLI “Hello from Default Package”.
How to compile and run java program form command line interface How to Compile and Run your java code without any error from cmd or terminal window
How to compile and run java program form command line interface

2. User Defined Package:

 

User Defined Package is nothing then a directory / folder where you want to organize your Java class. Lets create a directory called “core” inside our current directory i.e. “D:workspacejavaacpsrc”. Now create a java file with the name as “P1_HelloWorld.java” inside “core” directory and copy the below code into it, so the absolute path of the java file is “D:workspacejavaacpsrccore”.

 

Now follow these steps to compile & run your package class from command line interface as:

  1. Make sure your current working directory on CMD/Terminal is root directory where the package present. For example in our case it should be inside “D:workspacejavaacpsrc” not inside “D:workspacejavaacpsrccore”.
  2. Use javac tool followed by the package name then java file name as “javac core/P1_HelloWorld.java”, this will create a P1_HelloWorld.class file inside the directory “D:workspacejavaacpsrccore”.
  3. Now similarly run java tool followed by the package name then class name as “java core/P1_HelloWorld”. If JVM successfully able to interpret it will print a statement “Hello World from package” on CLI.
How to compile and run java program form command line How to Compile and Run your java package code without any error from cmd or terminal window
How to compile and run java program form command line

So, we have covered the basic steps need to followed by any programmer who want to compile and run their program from the command line interface. Now let’s discuss the issues can comes across while you performing these steps and their exact solution.

 1. CMD/Terminal/CLI throw an error for ‘java’ or ‘javac’ is not recognized as an internal or external command, operable program or batch file while compile or run Java code. What should I need to do?

If you ever see this type of error message on CLI, it means either you didn’t install JDK(Java Development Kit) on your system. If you are sure about your JDK is installed on your system but still face this issue it means your system path is not set to the JDK directory. To set system path on window simply go and click right click on “My Computer” or “This PC” and click on property, then go to “Advanced system settings” and click on “Environmental Variables”. Now inside system variable click on “Path” and edit it and append it with your JDK bin directory as “D:installationstudylanguagejavabin;”. Make sure to separate two entry by semicolon in window machine.

How to set system path or environment variable into window machine How to set system path inside environmental variable on window machines
How to set system path or environment variable into window machine

2. CLI throw an error “error: Could not find or load main class HelloWorld.class” while execute my java program using command prompt?

You can see this error in following scenarios :

  •  If you didn’t compile your code successfully due to which .class file not generated in the file system which cause this error by JVM. In this case you should first compile it successfully as above steps given.
  • If you have already successfully compiled your code and see the .class file but still its throw this error, this mean you might provide full class name with .class extension. You should execute your program by using command as “java ClassName” not as “java ClassName.class”.
  • If you are working with “Package” you make sure to compile and execute your command from root directory not from inside directory. You should use command “javac PcakageName/ClassName.java” to compile and “java PcakageName/ClassName” to run your program.

3. CLI throw an error “error: Class names, ‘HelloWorld’, are only accepted if annotation processing is explicitly requested”, how to resolve this java error?

You might try to compile your java class without giving proper name. If suppose we have a java file called HelloWorld.java then it should be compiled as “java HelloWorld.java” not as “java HelloWorld” or “java helloworld” etc.

4. Why this error thrown by javac compiler “HelloWorld.java:2: error: class HelloWorlds is public, should be declared in a file named HelloWorlds.java”?

It means your class name and file name not matched. Java is case sensitive language you should name your your file name as class name followed by the .java extension. In this example we should change the class name as “HelloWorld” instead of “HelloWorlds”.

Leave a Reply