Java command line compiler source code. java File __java

Source: Internet
Author: User

Java Command-line compilation class

Did the development of the Java EE also have some time, today suddenly thought of learning in the University of the use of command line to compile Java, so at night I wrote a simple example, the result is still encountering some problems. Oh, it seems that the IDE is used more.

One, Configure environment Variables

1. Configure Java_home

Depending on the installation location of the JDK, configure this parameter, for example:

D:/program files/java/jdk1.6.0_17

2. Configure Classpath

The role of Classpath is to tell the Java (or javac) command the location of the classes needed in the current compilation process, configure this variable and execute the Java command without having to specify the jar package location again, my machine configuration is as follows:

%java_home%/lib/dt.jar;%java_home%/lib/tools.jar;

3. Configure path

In cmd mode, we execute the Java command and the Javac command, and if all can be done, we do not need to configure it again, indicating that it was previously configured successfully. If the Java command does not execute successfully, add an entry in path (add a new item after the value of the previous path), as follows:

D:/program files/java/jdk1.6.0_17

If the Javac command does not execute successfully, add another entry in path, as follows:

D:/program Files/java/jdk1.6.0_17/bin

The correct execution is as follows:

C:/users/administrator>java-version

Java Version "1.6.0_24"

Java (TM) SE Runtime Environment (build 1.6.0_24-b07)

Java HotSpot (TM) Client VM (build 19.1-b02, mixed mode, sharing)

C:/users/administrator>javac

Usage: Javac < options > < source files >

Among these, possible options include the following:

-G generates all debugging information

-g:none does not generate any debugging information

-g:{lines,vars,source} generates only some debugging information

-nowarn does not generate any warnings

-verbose output messages about the operation being performed by the compiler

Second, write Java source code and compile

1. write Say.java source code

Create a new say Java file with the following code:

package com.csdn.test;

Public class Say {

/**

* @param args

*/

Public Static void Main (string[] args) {

System.out.println ("I say a word.") ");

}

}

2. compile Say.java source code

Execute the javac command to compile the source code, the blue section below explains that Javac execution succeeded and generated the Say.class file in the current directory as follows:

Microsoft Windows [version 6.1.7600]

Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:/users/administrator>d:

D:/>CD Temp

D:/temp>javac Say.java

3. Execute the compiled say.class file

The Java command was invoked to execute the Say.class file, and an error was found with the following error message:

D:/temp>java Say

Exception in thread "main" Java.lang.NoClassDefFoundError:Say (wrong Name:com/csdn/test/say)

At Java.lang.ClassLoader.defineClass1 (Native method)

At Java.lang.ClassLoader.defineClassCond (Unknown Source)

At Java.lang.ClassLoader.defineClass (Unknown Source)

At Java.security.SecureClassLoader.defineClass (Unknown Source)

At Java.net.URLClassLoader.defineClass (Unknown Source)

At java.net.urlclassloader.access$000 (Unknown Source)

At Java.net.urlclassloader$1.run (Unknown Source)

At Java.security.AccessController.doPrivileged (Native method)

At Java.net.URLClassLoader.findClass (Unknown Source)

At Java.lang.ClassLoader.loadClass (Unknown Source)

At Sun.misc.launcher$appclassloader.loadclass (Unknown Source)

At Java.lang.ClassLoader.loadClass (Unknown Source)

Could not find the main class:say. Program would exit.

4. Correct implementation of the Say.class method

Why the top "Java Say" command will quote the wrong name "Com/csdn/test/say". Let's recall how we deployed a Com.csdn.test.Say class to the application's classes directory. Each package needs to be placed under a folder, as is the Java command line. We set up the Com/csdn/test directory in sequence in the current directory, after the compiled Say.class copy to the test directory, we then execute, the implementation process is as follows:

D:/temp>java Com.csdn.test.Say

I said a word.

D:/temp>

As in the red section above, the main method in Say.class was successfully executed.

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.