Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use javac and java in cmd

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how cmd uses javac and java, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

1. Brief introduction:

In cmd, the difference between executing java commands and javac commands:

Javac: is a compile command that compiles java source files into .class bytecode files.

For example: javac hello.java

The hello.class file is generated.

Java: the bytecode file is run; the bytecode is interpreted and run by the java virtual machine.

For example: java hello

2. To elaborate on:

Let's try to write a java file.

1. Open notepad and enter a class + main function

2. Click File-> Save to save to the appropriate folder, and the file name has been modified xxx.javapublic class Hello {public static void main (String [] args) {System.out.println ("Hello World");}}

3. In the Hello.java folder, in a blank space, hold down the keyboard Shift, right-click the mouse, and select "Open Command window here"

4. Enter javac Hello.java to enter.

If there is no error in the writing format of the java class, cmd will not report an error and a class file will be generated in the same directory as the java file.

5. Enter java Hello to enter.

If you output the following picture, congratulations, it's a success! You have displayed the output print "Hello World" in the java class on the cmd command line.

III. Summary:

First of all, we use notepad to write the java source code with the extension. Java

In command line mode, type the command: javac source file name .java, compile the source code, and generate the class bytecode file

After the compilation is completed, if there is no error message, enter the command: java Hello, interpret the class bytecode file and print "Hello World".

To make it easier for you to understand the compilation and running process of javac and java, you can look at the following figure:

So far, I believe you should have a more objective understanding of the use of javac and java.

4. Answer questions:

Next, let's take a look at the problems that we are easy to encounter in practice.

Problem 1: the cmd command must be accurate, case-sensitive, and pay attention to whether or not to add an extension.

Javac Hello.java

Java Hello

Question 2: the javac command reports an error, as shown in the following figure

The javac command reports an error. Generally, the java code is incorrect and needs to be checked for correction.

In the following figure, there is an obvious error in the java code. The output print statement is written on the outside of the main function. If the modification is correct, the error will not be reported.

Javac command error, look at the java code, there must be an error.

Question 3: the execution of the javac command is completed and the class file is generated, but the java command reports an error, as shown in the following figure

I consult the data to find the reason for the error, first check the java source code, remember, do not add the package path to the java class! Then there is a problem with the java environment variable, let's check it together.

Right-click computer-Properties-Advanced system Settings, and click Environment variables. I'm sure you've all configured the java environment variable, so let's check it out. If you don't configure the java environment variable, look at the java development configuration environment variable.

Check "JAVA_HOME", "Path" and "CLASSPATH" one by one

(1) "JAVA_HOME", the variable value is the installation path of JDK on your computer, and mine is "D:\ java\ tool\ java1.8\ JDK"

(2) "Path", adding ";% JAVA_HOME%\ bin;%JAVA_HOME%\ jre\ bin" at the end of the value of the original variable

(3) "CLASSPATH", variable value ".% JAVA_HOME%\ lib;%JAVA_HOME%\ lib\ dt.jar;%JAVA_HOME%\ lib\ tools.jar"

Pay special attention to the fact that the variable value begins with ".;" (dot semicolon), which requires special attention. I report an error because this dot and semicolon are not correct.

After the java source code is modified, you need to delete the previously generated class file and run the javac and java commands again.

After the environment variable is modified, be sure to restart the cmd command line window, delete the previously generated class file, and rerun the javac and java commands.

Note: if you enter the javac command and show that 'javac' is not an internal or external command, it is also because the JDK development environment is not installed in advance or the configuration of the environment variables is incorrect.

Thank you for reading this article carefully. I hope the article "how to use javac and java in cmd" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report