Compilation run without package and package in Java

Source: Internet
Author: User

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

One, without package program
Create a Helloworld.java file, put in C:
public class helloworld
{
   public static void Main (String args[])
  //write String to screen " helloworld! "
    {
       System.out.println ("helloworld!");
   }
}

1. Compiling
      compiled instructions are Javac
      Compilation method: File root javac filename. java
example: This file name: helloworld.java
           File path: c:\
      compilation process:     < BR style= "Word-wrap:break-word;" >cmd Enter &NBSP;
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:

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[])
  //write String to screen " helloworld! "
    {
       System.out.println ("helloworld!");
   }
}

1. Compiling
      compiled instructions are Javac
      Compilation method: File root javac filename. java
example: file name: helloworld.java
           File path: c:\net\test\helloworld.java
           Package Name: NET.TEST
compilation process:
cmd Enter
cd c:\net\test Enter
javac Helloworld.java Enter     will generate a Helloworld.class file   

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!

Compilation run without package and package in Java

Related Article

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.