Grab Deal : Flat 23% off on live classes + 2 free self-paced courses as a bonus! - SCHEDULE CALL
Having Java installed on your system allows you to run and possibly compile Java programs. If your system does not know where to look for the command you have entered then probably it will show the error-
java: command not found
When you are trying to run the “Java” command, but the compiler is showing the above error, this means the command is not found in the shell search path, or there may be other possible reasons which will be discussed in this blog.
Human beings operate a Computer with the help of commands. While writing the software program, you may have dealt with several commands. If your program shows command not found an error, then it means that the computer is unable to find the program by that name. Before you ask your administrator for help, please consider the following points-
Fortunately, all the errors mentioned above can be resolved easily and get the common working again as expected.
When you are trying to run the Java code using Java IDE or command prompt, you may encounter the error java command not found. This error can occur while running Java on any of the platforms like Windows, Linux, or Mac. The solution to this error is the same for all the platforms. Only it varies at-
Solution: Download and Install the latest version of Java in your system using the internet.
Read: What is Java String? How to Declare String Array in Java with Example
Now, in order to make sure that Windows is able to find Java compiler and interpreter, open Command Prompt (cmd) and do the following-
Now, to check the version of Java, type the following boldface text. You will see the similar information mentioned below-
C:\Users\username>java -version java version "1.6.0_27" Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0_27-b07) Java HotSpot(TM) Client VM (build 1.6.0_27-b13, mixed mode, sharing)
C:\Users\username>javac -version javac 1.6.0_27
Now, you can compile your Java program using javac command to convert your Java program into an amendable form of a computer. You can use cmd or ide for this.
C:\Users\username>cd c:\introcs\hello C:\introcs\hello\>
If your file HelloWorld.java is in the current working directory, then type-
C:\introcs\hello\>javac HelloWorld.java C:\introcs\hello\>
Now, as everything went well with no error messages, you are ready to execute the Java program.
C:\introcs\hello\>java HelloWorld Hello, World
The output of your program will be Hello, World
Read: How Long Does It Take To Learn Java?
Solution: Check out the values of the path variable using Command Prompt with the following command-
C:\introcs\hello\> echo %PATH%
The path should begin with
Ensure that you have reopened Command Prompt after you have edited the PATH environment variable. You might need to reboot the system to see the modification of environment variable.
You can make the path of your JDK permanent in your system by following these steps, but PROCEED WITH EXTREME CAUTION-
Solution: A typo is a misprint or a spelling mistake made in a while typing. After all, the one who is giving a command to the computer is a human being. If you are making use of IDE, then you can use several editors available to write Java programs. Editors like SublimeText, Eclipse, NetBeans, IntelliJ Idea, Dr.java, JCreator, AndroidStudio, etc. are few of the Java editors which you can use to avoid typos. While writing a Java program in these editors, you will get an “autocomplete” suggestion for the usage of every Library function or the pre-loaded classes available in Java compiler. This autocomplete feature in IDE will help you from making silly typos. For example, if you are writing some looping structure and you are not confident about the syntax. Then start typing a loop (let’s say foreach loop), you will see just when you start typing the name of the loop, you will be able to see related suggestions with it. Now, you can choose the loop you want to choose, and it will complete the rest of the syntax. Only, you need to add your functions and values to it. Smart enough, right!
Read: How to Call a Method in Java?
Solution: Well, if you get a “command not found” error while writing a Java program, then check initially in which platform you are compiling in. Whether it is command prompt or IDE.
Solution: While compiling a Java program, the most common issue a programmer gets is “Javac is not recognized as an internal or external command”. You can fix this issue in the following ways: -
c:\>set path Path=C:\Program Files (x86)\Windows Resource Kits\Tools\; C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem; C:\WINDOWS\System32\WindowsPowerShell\v1.0\; C:\Program Files (x86)\Quarantine\; C:\Program Files\Perforce; C:\Program Files\Microsoft Network Monitor 3\; c:\Program Files\Java\jdk1.7.0_17\bin
If the JDK path is not added, then you can add by the following command on command prompt. Check the folder “C:\Program Files\java” and “c:\Program Files (x86)\java” to find the exact path. If you already have PATH set with the java location, check one more time if you have added path till the bin folder. So, adding
c:\Program Files\Java\jdk1.7.0_17 is not correct. You need to add
Solution: If javac error is there, then check for the proper syntax of all the Java components you have used. Sometimes, there can be a mistake while writing the syntax of Java commands. For instance, if you try to create an if statement that does not include the condition in parentheses, but you have mentioned the condition statement in the same line as if statement, this is a syntax error. There can be other syntactical errors also-
Solution: Another reason why your compiler is unable to recognize the command you have entered is that it might not have recognized or detected JVM. Java Virtual Machine (JVM) is the engine that drives the Java code. It is responsible for the conversion of Java bytecode into machine language. In this case, you only need to check the path of JDK by following these steps: -
Solution: Possible reasons, in this case, maybe-
Above are some of the possible reasons and the solutions for the Java error – “command not found.” You can learn more important concepts of Java programming like Exception handling, Multithreading, Java with Networking, Java Servlets, Java Frameworks (Struts, Hibernate, etc.), and can perform sample projects and get the hands-on experience with Java at JanBask Training. Enroll yourself now or talk to our counselor.
Read: What is the Future Scope of Java Developer & Programmer
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
MS SQL Server
Serialization of Java Objects to XML Using XML Encoder/Decoder 754.3k
How to Call a Method in Java? 428.5k
What is A Java Constructor? Type of Constructors & Uses in Java 455.6k
What Is Spring Framework? Spring Tutorial Guide for Beginner 267.1k
Top 110+ Java Interview Questions for Freshers & Advanced Professionals 206.8k
Receive Latest Materials and Offers on Java Course