HelloWorld question set
There are too many people asking HelloWorld questions, and they often start with "Ask the simplest question. Its
In retrospect, I also came from this stage and said "hello", which is really the simplest problem.
?... // Think: let's say "HelloWorld! "In java...
First, let's assume that our platform is Windows + JDK (similar in Linux ). This environment
Is quite common, basic, and entry-level. Confirm that JDK has been correctly installed. The next step is to carefully import a course
On the HelloWorld source code, save the disk, and then compile, javac... the problem is:
* Error 1:
'Javac' is not an internal or external command, nor a program or batch processing file that can be run.
(Javac: Command not found)
The cause is that the environment variable path is not set. Add in autoexce. bat under Win98
Path = % path %; c: \ jdk1.2 \ bin. in Win2000, choose Control Panel> system> advanced> environment variable> system change.
Volume... See? Double-click Path and add c: \ jdk1.2 \ bin to it. Of course, we assume that JDK is installed on
C: \ jdk1.2 directory (a bit down ?)... It seems that you have to restart the system to make it work... (// you know! // West
Persimmon)
Okay. Try again! Javac HelloWorld
* Error 2:
HelloWorld is an invalid option or argument.
Please give us some professionalism. The java source program must be saved as a. java file, and must be fully written during compilation.
. Java.
OK, javac HelloWorld. java (should this always happen ?)
* Error 3:
HelloWorld. java: 1: Public class helloworld must be defined in a file called
"HelloWorld. java ".
Public class helloworld {
^
This is because the name of your class is different from that of the file. (Who said, clearly seeing all people have
() OK. To be precise, multiple classes can be defined in a Java source program,
Only one attribute class can be created, which must be consistent with the file name. Also, the main method must be put in this public
To java (run. In addition, the Java language is case-sensitive.
For beginners, pay attention to it. As in the above example, helloworld and HelloWorld are considered different, so...
Oh... Okay, change it. Hey,... javac HelloWorld. java
... (Sorry, why is there nothing ?) // Faint. This is the compilation passed! Check if there is one more
HelloWorld. class?
(Hehe .. according to the instructions in the book :) java HelloWorld (!! I know that this is not java HelloWorld. class
Yo)
* Error 4:
Exception in thread "main" java. lang. NoClassDefFoundError: HelloWorld
Well, this is the famous classpath problem. In fact, the class path is in the compilation process
Java concepts involved. Classpath is the class used to specify where to find it, which is so simple. Because of our
HelloWorld does not use other (non-java. lang Package) classes, so this problem is not encountered during compilation. Run
You must specify where your class is. To solve this problem, run the following command:
Java-classpath. HelloWorld
"." Indicates the current directory. Of course, this is a little troublesome (it's too troublesome "!), We can
Set the default classpath. The method is as follows. Set classpath:
Classpath =.; c: \ jdk1.2 \ lib \ dt. jar; c: \ jdk1.2 \ lib \ tools. jar is also recommended.
For future development.
Java-classpath. HelloWorld (I will not learn java again)
* Error 5:
Exception in thread "main" java. lang. NoSuchMethodError: main
(// Stick to it. Look at your code. The problem lies in the definition of the main method. Is it correct,
Is it written like this:
Public static void main (String args []) {// do not make any difference in a word. Do not ask why...
Yes, including case sensitivity!
Java-classpath. HelloWorld !)
Hello World!
(Faint! Finally ...)
Welcome to the Java World! Therefore, the failure to run HelloWorld is really not a "simplest problem ".
Appendix: HelloWorld. java
// HelloWorld. java
Public class HelloWorld {
Public static void main (String args []) {
System. out. println ("Hello World! ");
}
}