Java and Javac compile run Java program

Source: Internet
Author: User

Catalogue

    • Directory
    • Objective
    • Introduction to compiling commands
      • Javac
      • Java
    • Code compilation and operation
      • Java class Discovery rules
      • Separating class and Java files
    • Reference links

Preface

Give yourself one months to learn the plan of the JVM virtual machine, whether or not to use eggs, learning something new is always good.

But in the course of learning, I would like to test some JVM parameters at the command line, I found myself out of the IDE, can not directly compile Java program!!

my principles can not complain too much, like a loser, will not take the time to make up.

Below, the main is to summarize some how to compile Java programs on the command line. The main focus is to write a summary of Java's class discovery principles .

Introduction to compiling commands

In fact, the Java compilation process mainly uses the Javac and Java two commands. Here's a look at how these two commands are used.

Javac

Javac is used to compile Java files into Byte-code class files. Here's how to use it:

javac [ options ] [ sourcefiles ] [ classes ] [ @argfiles ]

The options have several key parameters:

    • -D: Specifies where to store the compiled class file. By default, the generated class file and the source Java file are compiled in the same directory.
    • -classpath (-CP): Used to search for the class file required for compilation, indicating the location of the class file to be used for compilation.
Java

Java is used to execute programs in the following format:

java [options] classfile

The options generally need to specify the-classpath parameter, which specifies the location of the file to be executed and the path to the class to be used.

code compilation and Operation

A test case with package is presented here. Before everyone in the study Javac compile, are in the default package under the compilation, generally do not have problems, but encountered with packages of the class is not the same.
The sample directory structure is as follows:

Where SRC is the default package directory. We have added two test files under the SRC/JVM directory, which reads as follows:

    1. Src/jvm/compileclass.java
package jvm;publicclass CompileClass {    publicstaticvoidmain(String[] args) {        new TestClass1();        mTestClass1.sayHello();    }}
    1. Src/jvm/testclass1.java
package jvm;publicclass TestClass1 {    publicvoidsayHello() {        System.out.println("Hello World!");    }    publicstaticvoidmain(String[] args) {        new TestClass1();        mTestClass1->sayHello();    }}

At this time, if directly in the SRC/JVM directory, with Javac compiled Compileclass, error is as follows:

I think it is not possible to use Ecplise to click the Run button when you have finished writing such a simple code. That's the problem because ecplise, the compiler, hides Java as a class discovery rule.

Java class Discovery rules

In order to successfully compile and execute Java files, it is necessary to have classpath and package names for co-ordination.

Let's try disabling classpath first, and disabling the method is to specify when you run Javac
-classpath ""

In the SRC/JVM directory, we disable classpath, compile Testclass1.java:

"" TestClass1.java

After the implementation of the people, found that can be compiled through (as to whether the implementation of the people do not mind).

However, also in the SRC/JVM directory, we disable classpath to compile Compileclass.java, is not compiled through, compile errors and the same as before. The reason Complieclass.java can not compile through, because the Complieclass.java source code called TestClass1 This class, and disable Classpath, Javac can not find TESTCLASS1. Thus, we can summarize the following rules:

When you need to compile (or execute) Class A that references other classes such as B, the compiler needs to look for B in the directory specified in-classpath.
When the package is also specified in class B, the path of that B is Path=classpath + "/" + package (all ".") Converted to "/").

So, in the SRC/JVM directory, in order for Complieclass.java to be able to find the TestClass1 class at compile time, we need to set classpath like this:

".." CompileClass.java

When executing, the command is as follows:

".." jvm.CompileClass
separating. Class and. java files

After understanding the Java class Discovery rules, we should again optimize our compilation method.

Referring to eclipse, it creates a src directory and a bin directory in the root directory of the Java project, respectively. which

    • SRC directory: Store all. java files.
    • Bin directory: stores all compiled. class files.

We can easily specify a directory with the-d parameter to implement this feature. And, when we do this, the. class file generated by the Bin directory maintains the corresponding structure of the SRC directory, so we don't have to bother to think about the Classpath lookup class.

The sample procedure looks like this:

Reference Links

[1] Mastering the JAVA CLASSPATH

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java and Javac compile run Java program

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.