Java command-line compiler execution Program

Source: Internet
Author: User
Tags acer

Before you learn Java using the IDE, it is a good idea to use the command line Javac, Java to run a simple program, so you can familiarize yourself with the effect of package management on the path of the. class file.

Let's start by writing a simple code:

Package Com.csdn.lkasdolka;public class test_1{public    static void Main (string[] args) {for (int i = 0; i <; ++i) {    System.out.println (i);}}    }

Write well after saving for test_1.java,windows Environment Win+r Open command line, CD to Test_1.java file directory.

With the command


Compile the file, if the previous Java environment is configured, at this time in the command line to enter Dir we can see the directory is more than one Test_1.class file:

2014/10/06  16:23    <DIR>          2014/10/06  16:23    <DIR>          . 2014/10/06  16:23               454 test_1.class2014/10/06  16:09               173 Test_1.java               2 files            627 bytes               2 directories 13,711,450,112 bytes Available


Next use

Java test_1

An exception occurred while trying to run the bytecode file.

Exception in thread "main" java.lang.NoClassDefFoundError:test_1 (wrong name:cn/bupt/d10_1006_1/test_1) at JAVA.L Ang. Classloader.defineclass1 (Native Method) at Java.lang.ClassLoader.defineClass (classloader.java:800) at JAVA.S Ecurity. Secureclassloader.defineclass (secureclassloader.java:142) at Java.net.URLClassLoader.defineClass ( urlclassloader.java:449) at java.net.urlclassloader.access$100 (urlclassloader.java:71) at Java.net.URLClassL Oader$1.run (urlclassloader.java:361) at Java.net.urlclassloader$1.run (urlclassloader.java:355) at Java.secur ity. Accesscontroller.doprivileged (Native Method) at Java.net.URLClassLoader.findClass (urlclassloader.java:354) A T Java.lang.ClassLoader.loadClass (classloader.java:425) at Sun.misc.launcher$appclassloader.loadclass ( launcher.java:308) at Java.lang.ClassLoader.loadClass (classloader.java:358) at sun.launcher.launcherhelper.c Heckandloadmain (launcherhelper.java:482) 
This is because we set up a place in the beginning of the code
Com.csdn.lkasdolka
The package. A package is a mechanism for managing namespaces in Java. We need to manually create the Com\csdn\lkasdolka in the current folder and then cut the Test_1.class into the Com\csdn\lkasdolka\ directory.

At this time, the CD back to the original working directory (the parent directory of COM), the command line input

Java com.csdn.lkasdolka.test_1
Or

Java com/csdn/lkasdolka/test_1
You should be able to see the correct output:

C:\users\acer\desktop\technology\test_java>java com.csdn.lkasdolka.test_10123456789c:\users\acer\desktop\ Technology\test_java>java com/csdn/lkasdolka/test_10123456789

Summary:

1.javac is used to compile. java files to generate. class files (bytecode files), which Java uses to execute. class files, and to remove the. class suffix when executing.

2. If the package is defined in the source file, the. class file needs to be placed in the corresponding directory of the package definition, or the Java command will be noclassdeffounderror exception


The end~

(If there is an error, please correct it)

Java command-line compiler execution 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.