In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article editor for you a detailed introduction of "Linux how to compile and run Java file", the content is detailed, the steps are clear, the details are handled properly, I hope that this "Linux how to compile and run Java file" article can help you solve your doubts, the following follow the editor's ideas slowly in depth, together to learn new knowledge.
The 1.javac command line javac is used to compile java source files and generate .class files. The common option options for javac [option] source1 are as follows:-classpath (which can be abbreviated to-cp): specify the classpath to use or the path to the jar package to use. -d: specifies the location of the .class file generated after the source file is compiled.
The 2.java command line java is used to run the generated .class file. In the form of java [option] classname [arguments]
The common option options are-classpath (which can be abbreviated to-cp): specify the classpath to use or the path to the jar package to use (without the .class suffix). [arguments]: parameters passed to the main function. Example:
Java-cp. / bin/project/ com.company.Test-Xms20m-Xmx20m-XX:+HeapDumpOnOutOfMemoryError3. The class file public class Test {public static void main (String [] args) {System.out.println ("Hello World")}} without package structure is compiled and executed in the current directory: javac Test.java runs java-cp. Test or java Test-cp. Specifies that the search class file from the current directory is already in the same directory as class, and the class file has no package structure. So just use the class name Test directly.
4. The class file with the package structure first creates the java project directory, named project. Mkdir project
Create src and bin directories in the project, which are used to store ".java" files under src and ".class" files under bin. Cd project mkdir src bin
Create the package com.company under src. Mkdir-p src/com/company
Write a java file named Hello.java in the package. Vi src/com/company/Hello.java
Package com.company public class Test {public static void main (String [] args) {System.out.println ("Hello World");} create a directory under the bin directory to store the class files generated in the project project. Mkdir bin/project
Compile the java file and store the generated compilation file under bin/project, where the com/company directory is automatically created because of the "package com.company;" statement. "- d" specifies the location where the class file is generated. Javac. / src/com/company/Test.java-d. / bin/project/
Execute the java file. "- cp" indicates classpath, followed by a path, and specifies where it is created, otherwise an error will be reported. Specify the package name after that. The class name can be run. Java-cp. / bin/project/ com.company.Test successfully output the result
4. Error will be reported if there is a .class or file path error in the command:
Error: Could not find or load main class Test.class read this, this article "how to compile and run Java files in Linux" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.