Last Updated:

How to run a Java program from the command line

It happens that you need to run a program written for software from Oracle-Java. This is done using the command line in Windows. And as a rule, the process does not cause problems. But the question arises as to how to do this – not everyone is familiar with such operations. Therefore, you need to figure out how to open a Java program package in "cmd" under Windows.

When there is already a ready-made file written in .jar format, then you can proceed to a small and simple algorithm. To execute it, you only need the command line, as well as the location of the file required for launch. The rest is nothing needed to work.

Actions themselves:

  • Open a command prompt ("cmd.exe" in the menu with Start applications);
  • Enter the directory where the Java program is located, which you need to download for work;
  • It remains only to write the command "java (filename).jar". After that, the created program will be launched.

In general, there is nothing complicated in such a process. Therefore, users will be able to easily work with the created .jar files on the command line. No difficulties should occur with the correct execution. However, there are rare exceptions.

Java file does not start (does not open)

In very rare cases, there is a problem when it seems that everything is done correctly, but an error pops up in the command line that prevents the opening of a file written in Java. We need to figure out what this may be connected with.

The main reasons are:

  • Incorrect installation of Oracle software or its version is very outdated;
  • The file has a different directory, which is why it cannot be detected. It happens that the name of the program is also used in an incorrect form.

The last reason is the inattention of the user. However, this is easy to fix – you just need to rewrite the code on the command line. After that, if everything works, the problem will be confirmed - it consists in incorrectly entered code details. This is corrected individually and does not require additional explanations - it is enough just to pay attention to the algorithm for executing the program in "cmd".

But the first case will force you to devote a little time to reinstalling the component to work correctly with programs. To do this, a few minutes and a stable Internet connection are enough. The procedure itself is not very long and not confusing in any way:

  1. If Java software is installed at all, then it must be removed from the Control Panel (section called "Programs and Features");
  2. Then you need to go to the official website of the developer (Oracle) and find the appropriate version of the software for your system, and then download it. All that remains is to install the downloaded package;
  3. When everything is reinstalled, the person can try running their program again at the command prompt.

However, you can simply update Java if such a message appears in the taskbar. You can click on it and get a new version. Consequently, no additional manipulations in the system will be required. But if the initial installation was carried out by failures - with errors, then the final result of the update will not bring anything. But in most cases, users get working software to execute Java programs.

Log in to the Java source folder

From the command line, it is also possible to enter the folder with the source Java code. This requires a special team. You do not need to enter the names of any files - just specify the directory, after which the required action will be performed. The command itself looks like this in structure:

cdC:\Documents and Settings\admin\Мои документы\Java\Name Program

After that, you will log in to the folder with the source code written in Java. Therefore, it is possible to further perform with it all the various actions that are required to adjust it. And what the user himself will do with it already depends on what he wants to do, because it will be his program.