一个关于创建java包的问题(真的无奈,求助)

csdndephi1 2004-09-13 05:13:47
一个关于创建java包的问题
问题愚蠢,都不好意思拿到这上面来问,但实在没有办法,我搞了一个下午了,都没有弄明白是怎么
回事情呢。

我用的是java2 sdk j2sdk-1_4_2_04
运行环境是win2000server
还安装了一个JCreator

这里有三个*.java文件,是21天学会java上的例程,
——————————————————————————————————————
package com.prefect.ecommerce;

import java.util.*;

public class Item implements Comparable {
private String id;
private String name;
private double retail;
private int quantity;
private double price;

Item(String idIn, String nameIn, String retailIn, String quanIn) {
id = idIn;
name = nameIn;
retail = Double.parseDouble(retailIn);
quantity = Integer.parseInt(quanIn);

if (quantity > 400)
price = retail * .5D;
else if (quantity > 200)
price = retail * .6D;
else
price = retail * .7D;
price = Math.floor( price * 100 + .5 ) / 100;
}

public int compareTo(Object obj) {
Item temp = (Item)obj;
if (this.price < temp.price)
return 1;
else if (this.price > temp.price)
return -1;
return 0;
}

public String getId() {
return id;
}

public String getName() {
return name;
}

public double getRetail() {
return retail;
}

public int getQuantity() {
return quantity;
}

public double getPrice() {
return price;
}
}

——————————————————————————————————————
ackage com.prefect.ecommerce;

import java.util.*;

public class Storefront {
private LinkedList catalog = new LinkedList();

public void addItem(String id, String name, String price,
String quant) {

Item it = new Item(id, name, price, quant);
catalog.add(it);
}

public Item getItem(int i) {
return (Item)catalog.get(i);
}

public int getSize() {
return catalog.size();
}

public void sort() {
Collections.sort(catalog);
}
}
——————————————————————————————————————
import com.prefect.ecommerce.Storefront; //1
import com.prefect.ecommerce.Item; //2

public class GiftShop { //3
public static void main(String[] arguments) { //4
Storefront store = new Storefront(); //5
store.addItem("C01", "MUG", "9.99", "150"); //6
store.addItem("C02", "LG MUG", "12.99", "82");
store.addItem("C03", "MOUSEPAD", "10.49", "800");
store.addItem("D01", "T SHIRT", "16.99", "90");
store.sort();

for (int i = 0; i < store.getSize(); i++) {
Item show = (Item)store.getItem(i);
System.out.println("\nItem ID: " + show.getId() +
"\nName: " + show.getName() +
"\nRetail Price: $" + show.getRetail() +
"\nPrice: $" + show.getPrice() +
"\nQuantity: " + show.getQuantity());
}
}
}
——————————————————————————————————————

ok,一点问题都没有,调试通过,并且生成文件夹com\prefect\ecommerce\
但是把上面第三个文件的1、2行的代码改成如下:

import com.prefect.ecommerce.*;

问题就出来了,提示的错误为:
C:\j2sdk14204\shicao\chapter6\GiftShop.java:7: cannot access Storefront
bad class file: C:\j2sdk14204\shicao\chapter6\Storefront.java
file does not contain class Storefront
Please remove or make sure it appears in the correct subdirectory of the classpath.
Storefront store = new Storefront();
^
1 error

Process completed.

真的傻眼,实在不知道是怎么回事情呢,

还有,当我把com\prefect\ecommerce\ 文件夹整个移到C盘下,即:

C:\com\prefect\ecommerce\ 然后设置一个环境变量calsspath,它的值分别设置成:
C:\com
C:\com\prefect
C:\com\prefect\ecommerce
时,其结果都是一样的,提示的错误是:
C:\j2sdk14204\shicao\chapter6\GiftShop.java:3: package com.prefect.ecommerce does not

exist
import com.prefect.ecommerce.*;
^
C:\j2sdk14204\shicao\chapter6\Storefront.java:15: cannot resolve symbol
symbol : class Item
location: class com.prefect.ecommerce.Storefront
public Item getItem(int i) {
^
C:\j2sdk14204\shicao\chapter6\GiftShop.java:7: cannot access Storefront
bad class file: C:\j2sdk14204\shicao\chapter6\Storefront.java
file does not contain class Storefront
Please remove or make sure it appears in the correct subdirectory of the classpath.
Storefront store = new Storefront();
^
3 errors

Process completed.


第一次玩java,很多东西都不明白,求助,而且实在是弄不出来,才到这里发这样弱智的帖子
...全文
241 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdndephi1 2004-09-15
  • 打赏
  • 举报
回复
我觉得是不是自己的机子有问题了,嗯,很难说,不过,
放在同一目录下的时候是没有问题的,
不管这个问题了,以后再说吧,

我想19号结帖
csdndephi1 2004-09-14
  • 打赏
  • 举报
回复
to楼上的,按照你的方法试了一下,但是根本不起什么作用呀,
其调试出的信息还是:

--------------------Configuration: j2sdk14204 <Default>--------------------
C:\j2sdk14204\shicao\chapter6\GiftShop.java:3: package com.prefect.ecommerce does not exist
import com.prefect.ecommerce.*;
^
C:\j2sdk14204\shicao\chapter6\GiftShop.java:7: cannot resolve symbol
symbol : class Storefront
location: class GiftShop
Storefront store = new Storefront();
^
C:\j2sdk14204\shicao\chapter6\GiftShop.java:7: cannot resolve symbol
symbol : class Storefront
location: class GiftShop
Storefront store = new Storefront();
^
C:\j2sdk14204\shicao\chapter6\GiftShop.java:15: cannot resolve symbol
symbol : class Item
location: class GiftShop
Item show = (Item)store.getItem(i);
^
C:\j2sdk14204\shicao\chapter6\GiftShop.java:15: cannot resolve symbol
symbol : class Item
location: class GiftShop
Item show = (Item)store.getItem(i);
^
5 errors

Process completed.
Ji秋风 2004-09-13
  • 打赏
  • 举报
回复
嘻嘻~~偶回家了。解决了可别忘了给分哦:)
Ji秋风 2004-09-13
  • 打赏
  • 举报
回复
根本不需要设置环境变量
解决方法:
1。把你com目录用winzip打包,把后缀改称jar,名称随便改了,如xx.jar也是可以的。
然后把xx.jar放到 ..\j2sdk1.4.2_04\jre\lib\ext目录下
此时你在开发的时候,import com.prefect.ecommerce.*;
就可以调用包内的类了。
程序运行的时候,记得把这个包拷贝到..\j2re1.4.2_04\lib\ext目录下。
一切Ok了~

2. 把你的com目录放到你开发的同一目录下,就OK了~~ 此时可能会用到
CLASSPATH=.
SilentEddy 2004-09-13
  • 打赏
  • 举报
回复
如果你的这三个文件都在同一个目录里的话,你的import根本就没有作用。
如果你将第三个文件存在不同的目录里面,那么你改成用*的就可以了。
vongood 2004-09-13
  • 打赏
  • 举报
回复
环境变量有问题。
csdndephi1 2004-09-13
  • 打赏
  • 举报
回复
to楼上的,不要告诉我去试试,你根本就没有调试,就在这里乱发言,我很讨厌这种,只说不做的人,除非你在你的机子上调试通过了,否则不要在这里乱说。。。。
事实告诉我在我的机子上classpath改成了C:\也是没有任何用的错误仍然存在
keerqin 2004-09-13
  • 打赏
  • 举报
回复
把环境变量该为 C: 试试
C:\com\prefect\ecommerce\时,会到C:\com\prefect\ecommerce\下再找com\prefect\ecommerce

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧