Java compilation with package and Java compilation without package

Source: Internet
Author: User

Transferred from: http://www.biostatistic.net/home.php?mod=space&uid=12734&do=blog&id=1952

Dazambo the Lord! Solve the problem that has been bothering for several days

Programs that do not have a package in Java are compiled in a different way than programs with packages.

A. Program without package
Create a Helloworld.java file and put it in C:
public class HelloWorld
{
public static void Main (String args[])
Write the string "helloworld!" to the screen
{
System.out.println ("helloworld!");
}
}

1. Compiling
The compiled instructions are Javac
Compile by: File root directory javac filename. java
For example: This file name: Helloworld.java
File path: \ C
Compile process:
CMD Enter
CD \ Enter
Javac Helloworld.java Enter

Note: A helloworld.class file will be generated at this time under \ c.

2. Running

Java HelloWorld Carriage return

Note: HelloWorld does not have a suffix.

Second, the package with the program

How do you use CMD to compile a package file that is typically done at the package level or using Eclipse-generated files?

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:

Under C:\net\test, create a Helloworld.java file 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[])
Write the string "helloworld!" to the screen
{
System.out.println ("helloworld!");
}
}

1. Compiling
The compiled instructions are Javac
Compile by: File root directory javac filename. java
For example: file name: Helloworld.java
File path: C:\net\test\HelloWorld.java
File 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 javac–d. File name. Java to generate the package directory.

2. Running
The runtime needs to note that the root directory of the file cannot be, is the upper directory of the package directory to the file--can be understood as the project directory, and then run, 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

The general case of the package name is mostly developed through the IDE!

Java compilation with package and Java compilation without package

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.