The first day learning JAVA

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
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)
... ...
"
(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.


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值