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);}} }
After writing, save the test_1.java,windows environment Win+r open the command line. CD to the Test_1.java file folder.
With the command
Compile the file. Assuming that the Java environment has been configured before, at this time enter dir on the command line we can see a test_1.class file under the folder:
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 Folders 13,711,450,112 bytes Available
Next use
Java test_1
An exception occurred while attempting to execute 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 due to the fact that at the beginning of the code we set up a
Com.csdn.lkasdolka
The package.
A package is a mechanism in which Java is used to manage namespaces.
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\ folder.
At this time, the CD back to the original working folder (the parent folder 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 run the. class file and remove the. class suffix when running.
2. Assuming that the package is defined in the source file, you need to place the. class file under the appropriate folder for the package definition, or the Noclassdeffounderror exception will be thrown when you run the Java command.
The end~
(If there is an error, please correct it)
Java command-line compilation Run program