Java是目前世界上最常用和最受欢迎的编程语言之一。它用于软件工程、数据科学等不同领域。与任何其他语言非常相似,这种语言的代码是在IDE(集成开发环境(Integrated Development Environment))中编写的。但是,有些用户可能不喜欢下载第三方软件来编写代码,因此更喜欢Microsoft Word或Notepad等内置文本编辑器。虽然这些可能允许您编写代码,但它们不能帮助您运行它。这是命令提示符(Command Prompt)可能对您有所帮助的地方。在本文中,我们将演示如何(Java)使用命令提示符(Command Prompt)。
虽然我们将专门讨论命令提示符(Command Prompt),但它的 macOS 对应物终端(Terminal)也存在此功能。Mac的过程与Windows的过程相同。作为先决条件,您需要在计算机上下载专用的Java编译器。您可以从Oracle网站下载Java 开发工具包(Java Development Kit)( JDK ),它作为编写(JDK)Java代码的最重要工具包跨平台运行。您必须先将JDK下载并安装在您的计算机上,然后才能开始该过程。
如何使用命令提示符运行(Command Prompt)Java程序
首先(First),您必须编写要运行的代码并将其保存为Java可执行文件。为此,您可以使用MS Word或记事本(Notepad)。例如,我们将编写一个给出输出“Hello World!”的简单代码。这就是代码的样子:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
用户应该确保正确的缩进,因为记事本(Notepad)和Word都不会帮助您,而且文件保存为“.java”文件而不是“.txt”文件。为此,请将您的文件命名为“Sample File .java”(您可以将文件命名为任何名称,只要文件扩展名为 .java 并从文件(File)类型中选择“所有文件(File)”。此文件将是保存在您喜欢的任何目的地的单独文件夹中。
现在,打开命令提示符(Command Prompt)并以管理员身份运行它。我们将使用“cd”命令将命令提示符(Command Prompt)上的操作目录更改为存储Java程序的目录。运行以下命令行:
cd Folder-destination[Java-program-folder]
您也可以使用Dir(Dir)命令检查您的Java程序文件是否存在于此目录中。现在,在您的计算机上找到JDK的路径,并通过以下命令行设置JDK的路径。(JDK)例如,如果您正在运行 64 位Windows版本(因为这是最常见的情况),并且您决定安装 64 位JDK文件,那么您的文件夹目标将是“C:Program Files\Java ”,除非您决定在安装过程中更改它。
set path=%path%;C:\Program Files\Javajdk
接下来,使用 javac 命令(Java compile)编译您的程序。
javac SampleFile.Java
您必须在为程序选择的文件名之后跟随 javac 关键字。你还不会看到任何事情发生,因为程序只是被编译并且还没有运行。最后,要运行该命令,请使用Java命令。
java SampleFile
如果您正在编译简单的程序并将它们全部放在同一个目录中,则不必为文件设置永久路径,但如果情况并非如此,那么您最好这样做。命令提示符(Command Prompt)会为该会话设置环境变量,但是一旦您关闭命令提示符(Command Prompt)并且会话超时,这些设置就会失效。因此,如果您想为以后的所有会话设置永久路径,可以按照以下步骤操作:
打开控制面板(Control Panel)并访问系统(System)和安全(Security)设置。您可以通过将控制面板(Control Panel)设置查看为Category来找到它们。或者,也可以通过Windows设置访问此页面。从左侧选项窗格中,选择高级系统设置(Advanced System Settings),然后单击系统(System)属性(Properties)框底部的环境变量按钮。(Environment Variables)
在System Variables下,您将找到Path变量。选择它并单击它正下方的Edit 。
在Edit Environment Variable框中,单击New,这将允许您将新目录添加到路径中。
在这里,粘贴您在上面编译Javascript时使用的路径,然后单击“确定”保存设置。
这些是您必须遵循的步骤才能使用命令提示符编译和运行(Command Prompt)Java程序。附带说明一下,用户应该记住使用Java 开发工具包(Java Development Kit)而不是Java 运行时环境(Java Runtime Environment),这可能是大多数正在阅读本文的Java程序员已经在他们的计算机上安装和运行的东西。(Java)
您可以从Oracle.com(Oracle.com)下载 JDK 。我们希望这篇文章能够对您有所帮助。
How to run a Java program from the Command Prompt
Java is one of the most commonly used and sought-after programming languages in the world right now. It is used in varied fields such as software engineering, data science, etc. Much like any other language, code for this language is written in an IDE (Integrated Development Environment). Some users may not, however, prefer downloading a third-party software to write code on and thus prefer built-in text editors like Microsoft Word or Notepad. While these may allow you to write code, they can’t help you run it. This is where the Command Prompt may be of help to you. In this article, we will be demonstrating how you can run a Java program using Command Prompt.
While we will be talking exclusively about Command Prompt, this functionality exists for its macOS counterpart, Terminal, too. The process is the same for Mac as it is for Windows. As a prerequisite, you’re required to download a dedicated Java compiler on your computer. You can download the Java Development Kit (JDK), which runs across platforms as the most important toolkit to writing Java code, from the Oracle website. You’ll have to have the JDK downloaded and installed on your computer before you can begin the process.
How to run a Java program using Command Prompt
First things first, you will have to write the code that you want to run and save that as a Java executable file. For this purpose, you can use either MS Word or Notepad. For example, we’ll be writing a simple code that gives the output “Hello World!”. This is what the code would look like:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
Users should make sure of proper indentations since neither Notepad nor Word would help you with that, and also that the file is saved as a ‘.java’ file and not as a ‘.txt’ file. In order to do so, name your file as ‘SampleFile.java’ (You can name the file anything, as long as the file extension is .java and from File type, select ‘All Files’. This file is to be saved in a separate folder in whichever destination you prefer.
Now, open the Command Prompt and run it as administrator. We will make use of the ‘cd’ command to change the operating directory on the Command Prompt to the one where your Java program is stored. Run the following command line:
cd Folder-destination[Java-program-folder]
You can check if your Java program file is present in this directory by using the Dir command too. Now, locate the path of the JDK on your computer, and set the path to the JDK via the following command line. For example, if you’re running the 64-bit Windows version (since that’s most commonly the case), and you decide to install the 64-bit JDK file, your folder destination will be “C:\Program Files\Java”, unless you decide to change it during installation.
set path=%path%;C:\Program Files\Javajdk
Next up, use the javac command (Java compile) to compile your program.
javac SampleFile.Java
You have to follow the javac keyword after the filename that you’ve chosen for your program. You won’t see anything happen just yet, since the program has only been compiled and not run yet. Finally, to run the command, use the Java command.
java SampleFile
If you’re compiling simple programs and housing them all in the same directory, you don’t have to set a permanent path for your files, but if the case is otherwise, then you will be better off doing so. The Command Prompt sets the environmental variable for that session, but those settings are forfeited once you close Command Prompt and your session is timed out. Thus, if you want to set a permanent path for all your future sessions, you can follow the steps below:
Open the Control Panel and visit the System and Security settings. You can locate them by viewing Control Panel settings as Category. This page can, alternatively, be approached via the Windows settings too. From the left-side options pane, select Advanced System Settings and click on the Environment Variables button on the bottom of the System Properties box.
Under the System Variables, you will find the Path variable. Select that and click on Edit, which is right below it.
In the Edit Environment Variable box, click on New, which will allow you to add a new directory to your path.
Here, paste the path that you’ve used above while compiling the Javascript and save the settings by clicking on Ok.
These are steps you have to follow in order to compile and run a Java program using the Command Prompt. As a side note, users should keep in mind to use the Java Development Kit for the procedure and not the Java Runtime Environment, which is something probably most Java programmers, who are reading this article, already have installed and running on their computers.
You can download the JDK from Oracle.com. We hope that this article was able to help you.