command line compile and run package with backage and jar

Source: Internet
Author: User

First, with the backage:

Write a class, which is encoded according to the level of the package, if you want to compile and run through Javac and Java commands at this time, you need to do the following:

Create a Helloworld.java file under C:\net\test, and put it in C:
package Net.test; Note: This first line is a description of the package, where the net behind the point is actually the meaning of the directory.
public class helloworld
{
public static void Main (String args[])
{
system.out.println ("helloworld!");
}
}

1. Compile
The compiled instructions are javac
Compile by: File root directory javac filename. java
example: File name: Helloworld.java
file path: c:\net\test\helloworld.java
Package Name: NET.TEST
compile process:
cmd enter
CD c:\net\test enter
javac Helloworld.java enter will generate a Helloworld.class file
Alternatively, you can pass the javac–d. File name. Java to generate the package directory (note: the-D and dot numbers have spaces before and after them).

2. Running
The runtime needs to be aware that the root directory of the file can not be the upper directory of the package directory of the file-it can be understood as the project directory (that is, the net sibling directory), and then run, before the file name needs to add the package name.
Run the process:
CMD Enter
C + Enter, go back to the root directory
Java Net.test.HelloWorld Carriage return


Two, with Jar:

(1) Compile the class with the jar package:
1. You can use the command JAVAC-CP *\*.jar classname.java //*\*.jar represents the path and name of the jar you need, and ClassName represents the class name. It is recommended to use this method; when there are multiple jar packages, there is ";" Separated. Example:javac-cp E:\licenseAPI.jar-d. Tglr.java

2. *\*.jar can be added to the environment variables such as: classpath=.;  C:\myJar\test.jar; C:\myJar\test.jar is a concrete example of *\*.jar. Note that the C:\myJar is not added to the environment variable, and if you add the directory where the jar is located, you will find that the compiler cannot find the required class. Because the ClassLoader of the class does not load the class file in the jar file under the classpath, it simply mounts the unpackaged class in that directory, and the class that is not packaged as a jar in subdirectories in that directory. This way can be used, if you like.
3. You can put the jar file under the%java_home%\jre\lib\ext\ directory, so that nothing is configured, but this is a worst practice. Because there will be more trouble in the future.


(2), the operation of the class with the jar package:

1. Use command: JAVA-CP.; * backage path Class name ; Example: JAVA-CP.; Licenseapi.jar cn Org.t est


command line compile and run package with backage and jar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.