开发者

java.lang.ClassNotFoundException when executing from cmd

I've written the below code in a text file and saved it as Exercise1.java:

import java.util.*;
public class Exercise1
{
 public static void main(String[] args)
 {
  char myChar;
  int myInt;
  System.out.println(myChar);
  System.out.println(myInt);

 }
}

then in cmd I write

javac D:\Projects\Exercise1.java

I get 2 errors both saying the declared variables might not be initialize开发者_Go百科d. But if I initialize the variables the above code generates the "Exercise1.class" file. And then when I write

java D:\Projects\Exercise1

I get java.lang.ClassNotFoundException. Now what I wonder are:

  1. Aren't primitive types initialized to default values in Java (I thought and read they did).
  2. Why can't I run my program and get the Exception as there do not not seem to be any mistakes?


The java command doesn't take a filename - it takes a classname. The name of your class is just "Exercise1" - if it were in a package, it might be something like "foo.bar.Exercise1". You then separately specify the classpath to tell the JVM how to find classes, e.g.

java -cp D:\Projects Exercise1

... or you can change directory and take advantage of the fact that without a classpath specified, the JVM will include the current directory in the classpath:

cd D:\Projects
java Exercise1

EDIT: I'd assumed you'd already fixed the errors in the code. Don't try to run the code before it compiles without errors. The errors are due to your uninitialized local variables. Only instance and static variables are given default values.


cd D:\Projects
java -cp . Exercise1


Not sure how you compiled your program, it should failed at the two following lines:

 char myChar;
 int myInt;

Because you haven't given any values.

Then, although you can execute this line to compile:

javac D:\Projects\Exercise1.java

If you do not change the classpath, you should be in the Projects folder, and then run

java Exercise1

so it prints the values of myChar and myInt


@Jon : He hasn't declared any packages as of now.

The most common problem is that classpath issue. Try changing the path to the said directory and then compile. Or try changing the class path from set classpat=address_of directory

You have to initialize a "local" variable else java will give error. Java does provide the default value to the variables but not for local variables http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html


to answer your first point:

Java does initialized to the default values only if the object of a class is instantiated. Since here you are not instantiating a class, they are not initialized. The following code will initialize the variables to the default values:

class Exercise{
    int myInt;
    char myChar;
    }

class Exercise1{
public static void main(String []a){
//Instantiating Exercise
Exercise e=new Exercise();
System.out.println(e.myInt);
System.out.println(e.myChar);
}
}

Here i have not initialized the variables but i have instantiated the exercise class. On instantiation, the values of the variables are set to the default values. If you are new to java and you are not familiar with classes, i suggest you read up some good books on Java.

To answer your second question, you can add your CLASSPATH and PATH to the environment variables. To do this go to system properties->advanced->environment variables.


After changing my local variables to static instance variables I could both able to get class file without having to explicitly initialize them to some values and run the program and get the default values printed. Thank you all for you effort.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜