Installed the JAVA SE Development Kit 7u45 from Oracle's website.
Then wrote the first 'HelloWorld" class in JAVA:
package ExamPractice;
import java.lang.*;
import java.io.*;
class HelloWorld{
public static void main(String [] args){
System.out.println("Hello World!");
}
}
There are a few issues during the first test run of the code:
(1) the javac didn't run at the first time.
reason: didn't set PATH to the java bin directory.
solution: added JAVA path to the environment variable PATH: right click 'computer'->'Properties'->Advanced system settings'->"Advanced"->Environmental Variables", added "C:\Program Files\Java\jdk1.7.0_45\bin". Here,
the folder is 'Program Files", not "Program Files(86)" because I chose to install the JAVA SE for Windows x64
(2) the java didn't run:
firstly, the class name in the code is 'Hellowworld", but the file name is HelloWorld. So, the javac HelloWorld.java (*have to use ".java" for compile: otherwise, errormsg:
"D:\***\Tests\Java\CertificationTests\ExamPractice>javac HelloWorld
error: Class names, 'HelloWorld', are only accepted if annotation processing is
explicitly requested
1 error)
error: Class names, 'HelloWorld', are only accepted if annotation processing is
explicitly requested
1 error)
gave Hellowworld.class. So, if I called 'java HelloWorld", it cannot find the corresponding class.
secondly, if the class does not locate in the current directory of DOS command window, need to set 'CLASSPATH" (by "set CLASSPATH", set by Environmental variables, or from "java -cp" command. Otherwise, error msg: "Error:
Could not find or load main class HelloWorld"
thirdly, if using 'package ExamPractice;" statement,
then to run the class, need to get to the upper directory, i.e. "cd ..", then run "java ExamPractice/HelloWorld". Otherwise, will get the error message:
"D:\****\Tests\Java\CertificationTests>java ExamPractice\HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: ExamPractice\HelloWor
ld (wrong name: ExamPractice/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
Exception in thread "main" java.lang.NoClassDefFoundError: ExamPractice\HelloWor
ld (wrong name: ExamPractice/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
... ...
"
(3) additional notes: the name of the library and class are case sensitive, so it
should be written accurately. Otherwise, cannot find the lib or class successfully.
Finally, it turns out, the two packages (java.io, and java.lang) are no need to be
explicitly in the code in order to run the HelloWorld program.
1732

被折叠的 条评论
为什么被折叠?



