开发者

error while calling dependent file

开发者 https://www.devze.com 2023-03-14 19:57 出处:网络
i created 2 files... namely Shirt.java and ShirtTest.java The code for the Shirt.java class is as follows--

i created 2 files... namely Shirt.java and ShirtTest.java

The code for the Shirt.java class is as follows--

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);


}

}

The code for the ShirtTest.java is as follows--

public class ShirtTest {

  public static void main (String args[]) {

  Shirt myShirt = new Shirt();

  myShirt.displayShirtInformation();

  } 
}

While i compiled the Shirt.java file.. it compiled with no errors and created a Shirt.class file.. but when i tried to compile the ShirtTest file.. it gave an error.. Which is as follows..

> C:\java>javac ShirtTest.java
ShirtTest.java:6: cannot find symbol
symbol  : class Shirt
location: class ShirtTest
  Shirt myShirt = new Shirt();
  ^
ShirtTest.java:6: cannot find symbol
symbol  : class Shirt
location: class ShirtTest
  S开发者_开发百科hirt myShirt = new Shirt();
                      ^
2 errors

What is the problem here?

p.s-both Shirt.java as well as ShirtTest.java are in the same folder


First: javac Shirt.java Then: javac ShirtTest.java After that you can run ShirtTest like that: java ShirtTest


Try putting your classes in packages. Using the default package is bad practice.


Make sure that the current directory is in the classpath when compiling the test. e.g.

javac -cp . ShirtTest.java

You can check the current classpath with

c:\java> set CLASSPATH

When you compile Shirt.java it doesn't have any dependencies outside of the core Java libraries so the classpath doesn't matter for that. That's why you only see a problem when you come to ShirtTest.java


Did you compile Shirt class?

Try :javac Shirt.java ShirtTest.java

0

精彩评论

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

关注公众号