简体   繁体   中英

Problem While Setting up JAVA fro the 1st time on Windows 7

I installed the J2SE 6.o version. Now I'm having a problem getting it to work right.

> C:\java\jdk1.6.0_25\bin

This is the path of the bin file, and I put this in the Path tab. In the environment settings.

What are the next steps that I have to take to run .java files from the command prompt?

Do I have to put something in the class-path tab too?

Let me elaborate my problem:

If I run and compile the below mentioned file called Shirt.java it works fine.

public class Shirt{
public int ShirtID=0;
public String description="-description required-";
public char colorCode='U';
public double price=0.0;
public int quantityInStock=0;


public void displayShirtInformation(){
    System.out.println("ShirtId:"+ShirtID);
        System.out.println("ShirtDescription"+description);
        System.out.println("Color Code:"+colorCode);
        System.out.println("Shirt Price"+price);
        System.out.println("Quantity In Stock"+quantityInStock);
    }
}

But if I run another file that calls the previous file, then problems crop up. The file that calls the previous file is as follows.

public class ShirtTest {

  public static void main (String args[]) {

  Shirt myShirt = new Shirt();

  myShirt.displayShirtInformation();

  } 
}

When I try to execute the second file, there are a few errors that crop up and no compilation takes place. I believe it has something to do with some problem with the environment variable Path declaration.

I would use an IDE, this avoid the need to

  • setup the path
  • check that all the classes you need have been compiled.
  • setup the classpath for the java

Instead all you need to do is hit the Run button and it does the rest.

It may even help you write/format the code and generate a toString() method, getters/setters and unit tests for it.

路径设置不会有任何问题,因为第一个Java文件正在工作,否则它将给出“无法将'java'识别为内部或外部命令”错误。

It is better to make sure that you do not have a CLASSPATH environment variable set. If it is not set, Java will by default look in the current directory for class files. As long as your Java source files are in the same directory (and not in a package) you should be able to compile and run them with simple commands:

javac Shirt.java
javac ShirtTest.java
java ShirtTest

If this complains with a NoClassDefFoundError , then try specifying the classpath on the command line using the -cp option:

javac -cp . Shirt.java
javac -cp . ShirtTest.java
java -cp . ShirtTest

(note that . means "the current directory").

See the Getting Started tutorial , which also has a section on common problems and their solutions.

When you get an error, please always copy & paste the exact error message, instead of just saying "I get some errors". The more specific information you give, the easier it is to understand what the exact problem it is and the better we can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM