紧急:Java中的打包问题 [英] Urgent : Package problem in Java

查看:54
本文介绍了紧急:Java中的打包问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ski.java文件。我把它手动放在一个名为package1的文件夹中。

然后我在package1之外创建了另一个文件夹,但在同一层次结构中命名为package1。

package1& package2在D:\ src目录中。
$ 2 $ b在package2中,我制作了Mainclass.java&尝试导入package1并在其中使用Ski类。



然后在命令提示符下,我已经在自己的文件夹中编译了Ski.java,使用以下命令

D:\ src \ package1> javac Ski.java

然后我试图在其文件夹中编译MainClass,如下所示:



D:\ src \ package2> javac MainClass.java



但它没有编译。

编译错误是package1不存在。



请给我一个解决方案。

I have a Ski.java file. I have put it manually in a folder named package1.
Then I made another folder outside the package1 but in the same hierarchy named package1.
Both package1 & package2 are in D:\src directory.
in package2, I made Mainclass.java & trying to import package1 and use Ski class in it.

then in command prompt, I have compiled Ski.java in its own folder, using following command
D:\src\package1>javac Ski.java
then I am trying to compile MainClass in its folder, like this:

D:\src\package2>javac MainClass.java

But it is not compiled.
Compile error is that package1 doesnot exist.

please give me a solution.

推荐答案

你需要添加命令的 -classpath 选项,告诉编译器 Ski.class 文件的位置。类似于:

You need to add the -classpath option to your command, to tell the compiler where the Ski.class file is. Something like:
D:\src\package2>javac -classpath D:\src\package1 MainClass.java


编译 MainClass 时,你需要确保编译器知道来自 package1的.class文件



尝试编译 MainClass 而不是这样;

When compiling MainClass you need to make sure the compiler knows about the .class file from package1.

Try compiling MainClass like this instead;
D:\src\package2\javac -classpath ..\ MainClass.java





添加一个类路径选项,你可以告诉编译器在哪里找到jar和类文件。



希望这会有所帮助,

Fredrik



By adding a classpath option you can tell the compiler where to find jars and class files.

Hope this helps,
Fredrik


这篇关于紧急:Java中的打包问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆