Run java programs windows

How to Run a Java Program from the Command Prompt

Java is one of the most commonly used programming languages. It is also an IDE-intensive programming language, with tight integration with Eclipse. If you are just starting out learning Java, here is a basic guide to help you start running the Java application from the command prompt (in Windows).

Installing the Java Development Kit (JDK)

Before you can run a Java program on your computer, you’ll need a Java compiler installed. That comes with the Java Development Kit, or JDK. It’s an essential tool for developing in Java on any platform. The JDK is not the same as the Java Runtime Environment, or JRE, which you’ll already have installed if you’ve ever used a Java application on your machine.

1. Download the JDK from Oracle’s website. Make sure not to download the JRE to Server editions.

2. Make sure you grab the Windows version of the development kit.

3. Run the installer normally.

Running a Java Program from the Command Prompt

1. Create a simple Java program like the one below using Notepad or another text editor. Make sure to save the file with the extension “.java” rather than “.txt.”

2. Open the Command Prompt from the Start Menu. You can also press Win + R, then type cmd.exe into the Run field.

3. Use the cd command to change your working directory to the directory containing your Java program. Mine is saved in my “Scripts” folder, but you can save your file anywhere you’d like on your system.

4. Locate the path to the version of the Java Development Kit (JDK) on your computer. If you’re running 64-bit Windows, that will typically be in “C:\Program Files\Java.”

5. Set the path to the JDK with the “set” command, as seen below:

You may need to change the directory path to reflect the current version of Java. Make sure you’re using the Java Development Kit (JDK) directory and pointing to the “bin” folder. The Java Runtime Environment (JRE) folder also contains a “bin” folder but doesn’t hold the Java compiler. If you get errors around the compilation, make sure you’re using the correct directory path.

6. Compile the Java program with the javac command, as seen below:

7. You should see absolutely nothing happen. But if you use the dir command, you’ll notice a new file in your directory ending in the “.class” extension. This indicates the program has been compiled.

8. Use the java command to run your program:

Setting Your PATH Permanently

The above command doesn’t set your Java compiler PATH permanently. It sets the environmental variable for that session, but that change will be wiped away when you close the Command Prompt session. Follow the steps below to change your PATH variable for all future sessions.

Читайте также:  Изменить имя пользователя linux ubuntu

1. Open Control Panel from the Start Menu and click “System.”

2. Click “Advanced System Settings” in the left menu pane.

3. Click the “Environment Variables…” button at the bottom of the System Properties window.

4. Select the “Path” variable, then click the “Edit” button.

5. Click “New” to add a new directory to your path.

6. Paste the directory path you used above into the text box. Again, make sure you’re using the Java Development Kit (JDK) directory and not the Java Runtime Environment (JRE) directory next to it, then click “OK” to commit to the change.

Conclusion

We ran an extremely simple Java program in our example. But you can initiate Java programs of greater complexity from the Command Prompt. Just use javac to compile a file ending in “.java,” then use the java command to execute the compiled program.

Alexander Fox is a tech and science writer based in Philadelphia, PA with one cat, three Macs and more USB cables than he could ever use.

How to Compile and Run your First Java Program

By Chaitanya Singh | Filed Under: Learn Java

In this tutorial, we will see how to write, compile and run a java program. I will also cover java syntax, code conventions and several ways to run a java program.

Simple Java Program:

Output: This is my first program in java

How to compile and run the above program

Prerequisite: You need to have java installed on your system. You can get the java from here.

Step 1: Open a text editor, like Notepad on windows and TextEdit on Mac. Copy the above program and paste it in the text editor.

You can also use IDE like Eclipse to run the java program but we will cover that part later in the coming tutorials. For the sake of simplicity, I will only use text editor and command prompt (or terminal) for this tutorial

Step 2: Save the file as FirstJavaProgram.java. You may be wondering why we have named the file as FirstJavaProgram, the thing is that we should always name the file same as the public class name. In our program, the public class name is FirstJavaProgram , that’s why our file name should be FirstJavaProgram.java.

Step 3: In this step, we will compile the program. For this, open command prompt (cmd) on Windows, if you are Mac OS then open Terminal.
To compile the program, type the following command and hit enter.

You may get this error when you try to compile the program: “javac’ is not recognized as an internal or external command, operable program or batch file“. This error occurs when the java path is not set in your system

If you get this error then you first need to set the path before compilation.

Set Path in Windows:
Open command prompt (cmd), go to the place where you have installed java on your system and locate the bin directory, copy the complete path and write it in the command like this.

Note: Your jdk version may be different. Since I have java version 1.8.0_121 installed on my system, I mentioned the same while setting up the path.

Читайте также:  Windows server 2012 консоль сертификатов

Set Path in Mac OS X
Open Terminal, type the following command and hit return.

Type the following command on terminal to confirm the path.

The steps above are for setting up the path temporary which means when you close the command prompt or terminal, the path settings will be lost and you will have to set the path again next time you use it. I will share the permanent path setup guide in the coming tutorial.

Step 4: After compilation the .java file gets translated into the .class file(byte code). Now we can run the program. To run the program, type the following command and hit enter:

Note that you should not append the .java extension to the file name while running the program.

Closer look to the First Java Program

Now that we have understood how to run a java program, let have a closer look at the program we have written above.

This is the first line of our java program. Every java application must have at least one class definition that consists of class keyword followed by class name. When I say keyword, it means that it should not be changed, we should use it as it is. However the class name can be anything.

I have made the class public by using public access modifier, I will cover access modifier in a separate post, all you need to know now that a java file can have any number of classes but it can have only one public class and the file name should be same as public class name.

This is our next line in the program, lets break it down to understand it:
public : This makes the main method public that means that we can call the method from outside the class.

static : We do not need to create object for static methods to run. They can run itself.

void : It does not return anything.

main : It is the method name. This is the entry point method from which the JVM can run your program.

(String[] args) : Used for command line arguments that are passed as strings. We will cover that in a separate post.

This method prints the contents inside the double quotes into the console and inserts a newline after.

Checkout these basic java programs before reading next topic:

How to run a JAR file on Windows 10 [QUICK GUIDE]

  • To run a JAR file on your Windows 10 device you certainly need to have Jave installed.
  • You’ll find below a path to the latest Java version in case you need it.
  • Then, you can find JAR file openers or executors, and we recommend some.
  • Using Command Prompt is also a handy solution if you don’t want to install third-party tools.

A JAR file is a Java archive package format that might include a Java program within it. While you can extract JARs with archive software such as 7zip, you can’t run a pure Java JAR app as you would with other programs in Windows.

Since that’s the case, we’ll cover how you can run a pure Java app JAR file in Windows.

How can I run a JAR file on Windows 10?

1. Add Java to Windows

The crucial thing you need upfront is to have Java installed. Without it, Java apps aren’t going to run in Windows. This is how you can install or update Java in Windows 10:

  • First, open Command Prompt(Admin) by pressing the Win key + R keyboard combination and entering cmd in the box.
  • Next, input java -version in the Command Prompt and press Enter. It will then provide further details on your Java version as below.
Читайте также:  Когда установили windows команда

  • The current version of Java is Version 8 Update 131. If you don’t have Java or the most up-to-date version, you can add it for free.
  • Run through the Java setup wizard. Then, open your browser to verify that the program is working.

2. Download a JAR file opener

You can open JAR files and hundreds of different formats with a single application. File Viewer Plus is a universal file viewer for Windows that can open and display over 300 different file types.

The new updated version has a plethora of features that will save you big time on opening, viewing, or editing files.

File Viewer Plus supports the most popular text, audio or video file type, as well as archive, source code or web files.

File Viewer Plus

3. Use Java(TM) Platform SE binary

  1. Open the File Explorer and the folder that includes the file you need to run.
  2. You should right-click the JAR file and select Open with from the context menu.
  3. Click Choose another app and then select to open it with Java(TM) Platform SE binary.
  4. Click the Always use this app to open JAR files check box.
  5. Press the OK button to close the menu.
  6. Now you can run the selected JAR, and all other JARfiles will also be associated with Java.

4. Run a JAR file from the Windows Command Prompt

  1. Press the Win key + X hotkey and select Command Prompt (Admin) to open it as administrator.
  2. Then input the following command (replacing the example with the actual path of the file you need to run) and press Enter: java -jar c:pathtojarfile.jar
  3. Alternatively, you can open the folder that includes the JAR in the Command Prompt first by entering cd / followed by the directory. Then, you can input java -jar file.jar without the folder path.

That will open the executable JAR file so long as it contains a manifest file to specify the application’s entry point. (The developers should have already included that in the archive.)

If you develop your own Java programs, you’ll need to convert the JAR to an executable format.

5. Add a third-party JAR executor to Windows

There are also executor programs for Windows that enable you to run JAR files the same as any other program . Jarx is one of them and you can add it to Windows by clicking Jarx-1.2-installer.exe on this page.

Click the jarx .exe to run the software, which doesn’t really have a GUI as such (except an About jarx window). Then you can double-click the JAR files to run them in Windows.

If you’re having trouble accessing the JAR file or you get some kind of an error, take a look at this nifty guide to finding some great fixes.

With all this, you can now run JAR files in Windows. If you need to fix a JAR file that isn’t opening, check out this Windows Report guide.

If you have any other suggestions or questions, leave them in the comments section below and we’ll be sure to take a look.

Оцените статью