Java and windows cmd

Java and windows cmd

This document instructs you on how to use the Windows Command Prompt with Java. These instructions are specialized to Windows 7, but are similar for Windows XP and Windows Vista.

    Download and install the latest version of the Java Platform, Standard Edition Development Kit (Java SE 6 Update 27). Note the installation directory for later—probably something like C:\Program Files\Java\jdk1.6.0_27\bin.

Select Start -> Computer -> System Properties -> Advanced system settings -> Environment Variables -> System variables -> PATH. Control Panel -> System and Security -> Programs -> Advanced -> Environment Variables -> PATH —>

Control Panel -> System and Maintenance -> System -> Advanced System Settings -> Advanced -> Environment variables -> PATH. ] —> [ In Vista, select Start -> My Computer -> Properties -> Advanced -> Environment Variables -> System variables -> PATH. ] [ In Windows XP, Select Start -> Control Panel -> System -> Advanced -> Environment Variables -> System variables -> PATH. ]

    Launch the command prompt via All Programs -> Accessories -> Command Prompt. (If you already had a command prompt window open, close it and launch a new one.) You should see the command prompt; it will look something like:

Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved.
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 
    Window Size to 80 x 25. —>

  • Set Layout -> Screen Buffer Size to 80 x 500.
  • Select Options -> Edit Options -> QuickEdit Mode.
  • Select Options -> Edit Options -> Insert Mode. Start in to «C:\introcs» or whichever directory you’d like to start in when launching the Command Prompt. —>
C:\Users\username>cd c:\introcs\hello C:\introcs\hello\>
C:\introcs\hello\>javac HelloWorld.java C:\introcs\hello\>

    From the Command Prompt, type the java command below.

C:\introcs\hello\>java HelloWorld Hello, World

If your program gets stuck in an infinite loop, type Ctrl-c to break out.

If you are entering input from the keyboard, you can signify to your program that there is no more data by typing Ctrl-z for EOF (end of file). On some DOS systems the first line of output sent to the screen after you enter EOF will be rendered invisible by DOS. This is not a problem with your code, but rather a problem with DOS. To help you debug your program, we recommend including an extra System.out.println(); statement before what you really want to print out. If anyone knows of a better fix, please let us know!

Here are a few suggestions that might help correct any installation woes you are experiencing. If you need assistance, don’t hesitate to contact a staff member.

When I type, «java -version» I get an error. Check that you edited your PATH environment variable as indicated. A missing ; or an added % is enough to screw things up. Close and re-open a command prompt. Type path at the command prompt and look for an entry that includes C:\Program Files\Java\jdk1.6.0_27\bin;. Check that the version number 1.6.0_27 matches the one you installed—Oracle updates Java periodically and you might have a more recent version. If this doesn’t fix the problem, check if you have any old versions of Java on your system. If so, un-install them and re-install Java.

The command «java -version» works, but not «javac -version». Any thoughts? It’s likely a path issue. Try the suggestions from the previous question. Also check that you installed the JDK properly by checking that the folder C:\Program Files\Java\jdk1.6.0_27\bin exists.

How can I check the values of my PATH variable? Type the following at the command prompt.

C:\introcs\hello\> echo %PATH% 

The PATH variable should begin with C:\Program Files\Java\jdk1.6.0_27\bin; Be sure to open the command prompt after you have edited the PATH environment variable. You may also need to reboot for the environment variable change to take effect.

I can compile with javac, but I get the error message «Exception in thread «main» java.lang.NoClassDefFoundError: HelloWorld» when I try to execute it with java. First, be sure that HelloWorld.class is now in the current directory. Be sure to type java HelloWorld without a trailing .class or .java. Check that the command «java -version» works. Now try to execute with «java -cp . HelloWorld«. If this works, you need to edit your classpath. (iTunes has a proclivity for changing the classpath, so if you recently upgraded iTunes, this is likely the source of the problem.)

Where can I learn more about the Windows command line? Here is a short tutorial on the Windows command prompt. Microsoft maintains a complete command line reference.

How do I change my directory to the H: drive from the Windows Command Prompt? Type H: at the command prompt. Then cd to the appropriate directory.

Источник

How to Run a Java Program from the Command Prompt

Featured Image Run Java Program From Windows 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. You can run Java programs from the Command Prompt for quick compiling and execution.

If you are just starting to learn Java, this basic guide will help you start running the Java application from the Command Prompt in Windows 10/11.

Content

Installing the Java Development Kit (JDK) in Windows

Before you can run a Java program on your computer, you’ll need to have a dedicated compiler installed. This comes within the Java Standard Edition Development Kit (JDK). It’s an essential tool for developing in Java on any platform.

The JDK is not the same as the Java Runtime Environment (JRE), which you’ll already have installed if you’ve ever used a Java application on your machine.

Java Windows Cmd Download Java Latest Version

  1. Download the JDK from Oracle’s website – the Windows version. Download any of the following: an x64 installer (shown in the screen), an x64 compressed archive, or an x64 MSI installer.

Note: if you have just simple use for Java software, make sure you do not download the “Java SE Development Kit for Java SE subscribers,” which is on the same download page. If you wish to use Java’s JRE installation for Microsoft Windows, it has been moved to another page.

Java Windows Cmd Download Java Latest Windows Installer

Java Windows Cmd Installer Installation Wizard Start

  1. Note the Windows location where Java is being installed. It will come in handy later when you’re trying to run Java from the Command Prompt.

Java Windows Cmd Java Location On Pc

  1. The installation should be over in just a few seconds. If it is taking a long time, close all of your other apps from Task Manager and reinstall the software.

Java Windows Cmd Installation In Progress

Java Windows Cmd Successful Installation Message

Running a Java Program From the Command Prompt

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }

Java Windows Cmd Java Program In Notepad

Java Windows Cmd Save Notepad File As Java Extension

Java Windows Cmd Open Command Prompt As Admin

  1. Use the cd command to change your working directory to the directory containing your Java program. To know which directory to go to, check the saved location of Java on your PC as discussed above.
cd Documents[Java-program-folder]
  1. From here, locate the path to the version of the Java Development Kit (JDK) on your computer. For example, if you’re running 64-bit Windows, that will often be in “C:\Program Files\Java.”

Java Windows Cmd Cd Change Directory To Java

set path=%path%;C:\Program Files\Java\jdk-"Java Version Number".bin
  1. 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.

Java Windows Cmd Cd Change Directory To Jdk Bin

Note: 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.

  1. Compile the Java program with the javac command as shown below. Be warned that you won’t see anything happen. However, if you use the dir command, you’ll notice a new file in your directory ending in the “.class” extension, indicating the program has been compiled.

Java Windows Cmd Test Java Program Command Prompt

Java Windows Cmd Java Prog Running

You’ll see the program run within the Command Prompt window, but there’s one more task you can do to make sure your Java program runs smoothly: set your path.

Setting a Permanent PATH

The above command doesn’t set your Java compiler PATH permanently. It sets the environment variable for that session, but that change will be wiped away when you close the Command Prompt session.

Setting your Java compiler PATH permanently can come in handy if you want your compiled Java programs to run smoothly after a PC reboot. This helps launch the requested programs quickly from the Command Prompt window (or a third-party software like Eclipse).

Follow the steps below to change your PATH variable for all future sessions.

Java Windows Cmd Cd Control Panel System

Java Windows Cmd Select Advanced System Settings

  1. Click the “Environment Variables” button at the bottom after you’ve switched to the “Advanced” tab in the “System Properties” window.

Java Windows Cmd System Properties Advanced Environment Variables Select

Java Windows Cmd Environment Variables Path Edit 2

Java Windows Cmd Environment Variables New Path

  1. 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.

Java Windows Cmd Environment Variables Permanent Path

This article featured a simple Java program, but you can initiate almost any Java program from the Command Prompt. The procedure is straightforward regardless of the nature of your program.

Frequently Asked Questions

How can I fix «You don’t have permission to save in this location» while saving Java files?

Some users may get a “You don’t have permission to save in this location” error while saving Java files in Windows. Even though you are the administrator of your PC, this error shows up out of nowhere. To fix the problem, right-click the Java folder’s “Properties” and navigate to the “Security -> Advanced -> Select User or Group.”

In the “Advanced Security” settings for Java, change the “Owner” from “System” to whatever user account you have used to log in to the device. You can determine the correct name from “Check names.” Just enter the text, such as “Desktop,” “Administrator,” or “Users,” to enable full permissions to the folder. Click “OK” and save the changes.

How can I fix «Java is not recognized as an internal or external command» in Windows?

The best way to fix “Java is not recognized as an internal or external command” is to add Java’s bin directory to your computer’s path, as covered above.

Windows Command Prompt doesn’t show the results of Java command. How can I fix it?

If your Windows Command Prompt doesn’t show the results of a Java command you’ve entered, there are two solutions: run the Command Prompt in Administrator Mode or find your “Java.exe” file in the folder location and open its “Properties.” Then, navigate to the “Compatibility” tab where you will have to uncheck the “Run this program as an administrator” option.

What is the difference between Java and Javascript?

Don’t confuse Java with Javascript, as they are two different entities:

  • Java came before Javascript. It was founded by Sun Microsystems in 1991-1995. Javascript was founded later by Netscape, an old browser company. Basically, Javascript is a very lightweight version of Java and still commonly used in browsers.
  • Java is a compiled program, whereas Javascript is interpreted.
  • Java is a static typed program, whereas Javascript is dynamically typed.
  • Java uses classes, and Javascript uses prototypes.

Image credit: WrightStudio via AdobeStock. All screenshots by Sayak Boral.

Sayak Boral is a technology writer with over eleven years of experience working in different industries including semiconductors, IoT, enterprise IT, telecommunications OSS/BSS, and network security. He has been writing for MakeTechEasier on a wide range of technical topics including Windows, Android, Internet, Hardware Guides, Browsers, Software Tools, and Product Reviews.

Our latest tutorials delivered straight to your inbox

Источник

Читайте также:  Time to long python
Оцените статью