HelloWorld FAQ

xiaoxiao2021-03-06  78

HelloWorld.java without package level

Public class helloworld {public static void main (string [] args) {system.out.println ("Hello World!");}}

Save Under the E: / Java / SRC, compile using the javac command:

E: / Java / SRC> Javac HelloWorld.java

run:

E: / Java / SRC> Java HelloWorld

Screen printing:

Hello World!

The mistakes of beginners are committed

1. Run, bring the .class suffix If you try to use the following command: E: / java / src> java helloWorld.class system misunderstand that you run the class files under the HelloWorld package, will go to the system Under the classpath (generally included the current directory) attempt to find a class such as helloworld.class.class, such a class must not exist; and it is impossible to exist because the Class is a keyword and cannot be used as a class name. So the following error message: Exception in thread "main" java.lang.noclassdeffounderror: HelloWorld / Class 2. File size case error For systems like Windows, you can compile cases. For example, when compiling HelloWorld.java, you can also use: E: / Java / SRC> Javac HelloWorld.java can also compile, but generated class files are still compliant with the source file: HelloWorld.class. But at runtime must pay attention to case, such as trying to use the following command: E: / java / src> Java HelloWorld will report the error similar to 1: Exception in thread "main" java.lang.noclassdeffounderror: HelloWorld (WRONG) Name: HelloWorld)

HelloWorld.java containing the package hierarchy, for example, HelloWorld.java, modified as follows:

Package org.javaresearch; public class helloworld {public static void main (string [] args) {system.out.println ("Hello World!");}}

There are two ways to compile

1. Directly compile E: / Java / Src> Javac HelloWorld.java outputs HelloWorld.class at the current directory. At this point, run cannot use the same method, use: E: / Java / SRC> Java HelloWorld running, the following error appears: Exception in thread "main" java.lang.noclassdeffounderror: HelloWorld (WRONG NAME: ORG / JAVARESEARCH / HelloWorld) You can also see from the above error message, the system can find the HelloWorld class (because the current path is included in the classpath, why is it prompting WRONG NAME, interested friends to see the Java language specification, but this class belongs to Org.javaresearch package. So, what you have to do is to create a directory level in accordance with the above package level, put the HelloWorld.class generated above in the E: / java / src / org / javaresearch / directory. Run: E: / Java / src> java org.javaresearch.Helloworld system print out: Hello World! This should be noted here, you can't run using java org / javaresearch / helloWorld, at this time, the following error occurs: Exception In Thread "main" java.lang.noclassdeffounderror: org / javaresearch / helloworld (WRONG Name: org / javaaresearch / helloWorld) Haha, is it a bit weird, that is no way. When you have a deeper understanding of Java's package, you will understand. 2. Use the -D whether it feels that the above compilation method is a bit trouble, can you automatically generate a packet level in the current path (or any specified path)? Have! You can do it using the -D Compile option. E: / java / src> javac -d. HelloWorld.java At this time, an ORG / JAVARSEARCH directory is generated in the current directory, and the output .class file is also in it. Run: E: / Java / SRC> Java Org.Javaresearch.Helloworld System Print: Hello World! If you want to put the generated class file in a directory, such as: E: / Java / Classes, then you first create This directory, then compile: E: / java / src> javac -d e: / java / class helloWorld.java can put the generated class file in the E: / java / class directory, and according to the package level Create a directory path. You can find the HelloWorld.class file under E: / Java / Classes / Org / JavaResearch. Use the following command to run correctly (note if you want to use other classes, set in ClassPath): E: / java / classes> java org.javaresearch.HelloWorldRef: http://www.weste.net/html / 200310/20031007qbi143242.html resolution:

D: / windows / java / packages> h: /j2sdk1.4.2/bin/java.exe com.wulliam.javawork.acompuspeoplehello world! First name: wu encountered questions:

Exception in thread "main" java.lang.NoClassDefFoundError: ACompusPeople (wrname: com / wulliam / javawork / ACompusPeople) at java.lang.ClassLoader.defineClass0 (Native Method) 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 $ 100 (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) at java.lang.classloader.loadClassinternal (Unknown Source)

转载请注明原文地址:https://www.9cbs.com/read-93634.html

New Post(0)