[Java Beginner] Problem to "Could not find the main class"
2013-04-09 14:14
447 查看
We are beginners of Java programming. We study Java development with JDK 1.6.0_35 and its shipped tools. We have already added the directory of Java utilities "Java\jdk1.6.0_35\bin" to the PATH system environment variable of Windows. Then we began the study
to Java.
We study Java with the book "First Head Java Second Edition". When we tried to compile and run a demo program in the book, we encountered a problem. The code is listed in the following:
Extracted from the book:
-------------------------------
public class Dog {
String name;
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.bark();
dog1.name = "Bart";
Dog[] myDogs = new Dog[3];
myDogs[0] = new Dog();
myDogs[1] = new Dog();
myDogs[2] = dog1;
myDogs[0].name = "Fred";
myDogs[1].name = "Marge";
System.out.print("last don't name is ");
System.out.println(myDogs[2].name);
int x = 0;
while (x < myDogs.length) {
myDogs[x].bark();
x = x+1;
}
}
public void bark() {
System.out.println(name + " says Ruff!");
}
public void eat() { }
public void chaseCat() { }
}
-------------------------------------
We typed the above code with Notepad, and then saved it as Dog.java. Then we compile the code with the javac in prompt:
F:\Programming\Java\Dog\javac Dog.java
The code is compiled successfully. But when we tried to run it, an exception raised:
The JVM told us that "Could not find the main class: dog."
Oops, the code could pass through the compiler, so the code should be able to run. What's the problem? Have no main class? We have already declared that!!! And the Dog.class file is existed in the program directory.
After checked the code again over again, about half an hour after, we finally found that the class name we want to run is wrong, it should be "Dog" not "dog". If we want to run the demo program, we should type in:
java Dog
Then, the miracle happened:
The Class name in Java is case-sensitive!!!!
We have searched for the above problem on the Internet, someone who told us that the problem is due to the JVM version is difference than the compiler version. Now we knew that a lilttle typos could also cause such errors.
Addition
If the above solution to the problem does not work, you may have to edit the system environment variables to add variable CLASSPATH, and set its value to include the string .\; into it. Note that, the string consists of a dot, a backslash and a semicolon.
Perhaps you should make such a string be presented in the beginning of the value for CLASSPATH. Like this:
By:
Ma Xiaoguang and Ma Xiaoming
to Java.
We study Java with the book "First Head Java Second Edition". When we tried to compile and run a demo program in the book, we encountered a problem. The code is listed in the following:
Extracted from the book:
-------------------------------
public class Dog {
String name;
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.bark();
dog1.name = "Bart";
Dog[] myDogs = new Dog[3];
myDogs[0] = new Dog();
myDogs[1] = new Dog();
myDogs[2] = dog1;
myDogs[0].name = "Fred";
myDogs[1].name = "Marge";
System.out.print("last don't name is ");
System.out.println(myDogs[2].name);
int x = 0;
while (x < myDogs.length) {
myDogs[x].bark();
x = x+1;
}
}
public void bark() {
System.out.println(name + " says Ruff!");
}
public void eat() { }
public void chaseCat() { }
}
-------------------------------------
We typed the above code with Notepad, and then saved it as Dog.java. Then we compile the code with the javac in prompt:
F:\Programming\Java\Dog\javac Dog.java
The code is compiled successfully. But when we tried to run it, an exception raised:
The JVM told us that "Could not find the main class: dog."
Oops, the code could pass through the compiler, so the code should be able to run. What's the problem? Have no main class? We have already declared that!!! And the Dog.class file is existed in the program directory.
After checked the code again over again, about half an hour after, we finally found that the class name we want to run is wrong, it should be "Dog" not "dog". If we want to run the demo program, we should type in:
java Dog
Then, the miracle happened:
The Class name in Java is case-sensitive!!!!
We have searched for the above problem on the Internet, someone who told us that the problem is due to the JVM version is difference than the compiler version. Now we knew that a lilttle typos could also cause such errors.
Addition
If the above solution to the problem does not work, you may have to edit the system environment variables to add variable CLASSPATH, and set its value to include the string .\; into it. Note that, the string consists of a dot, a backslash and a semicolon.
Perhaps you should make such a string be presented in the beginning of the value for CLASSPATH. Like this:
By:
Ma Xiaoguang and Ma Xiaoming
相关文章推荐
- Java中Could not find the main class.program will exit和java.lang.UnsupportedClassVersionError 的解决方法
- Debian配置JAVA环境报错之Could not find the main class: HelloWorld. Program will exit.
- java打jar包的异常一:could not find the main class,java.lang.UnsupportedClassVersionError: Bad version numb
- <综合>java virtual machine launcher 提示 could not find the main class,program will exit 问题
- jar包无法运行——could not find the main class...(java -jar *.jar可以运行,jar包不可以打开的解决方法)
- Unable to find a javac compiler; com.sun.tools.javac.Main is not on the classpath. Perhaps JAVA_HOME does not point to the JDK(转
- Unable to find a javac compiler; com.sun.tools.javac.Main is not on the classpath.Perhaps JAVA_HOME does not point to the JDK
- Java中Could not find the main class.program will exit和java.lang.UnsupportedClassVersionError 的解决方法 .
- Java Ant Could not find the main class: org.eclipse.ant.internal.launching.remote.InternalAntRunner. Program
- java调用本地方法的时候报错 could not find the main class:xx.program will exit
- 问题:Unable to find a javac compiler; com.sun.tools.javac.Main is not on the classpath.Perhaps JAVA_HOME does not point to the JDK
- Java执行main方法,异常为:could not find the main class.program will exit
- "Could not find the main class: org.apache.catalina.startup.Boostrap. Program will exit."
- 【java】报错could not find the main class,program will
- Unable to find a javac compiler; com.sun.tools.javac.Main is not on the classpath.Perhaps JAVA_HOME
- [java] Could not find the main class的问题处理
- java virtual machine launcher Could not find the main class. Program will exit
- 【java】报错could not find the main class,program will
- Java中Could not find the main class.program will