java包不存在和坏的源文件 [英] java package does not exist and bad source file
问题描述
所以我创建了一个名为util的文件夹,并在其中放置了四个类以及名为unit10Assignment的程序。我创建了一个包util并在每个类代码的顶部键入package util;,如下所示:
So I made a folder called util and placed four classes along with program named unit10Assignment in it. I created a package util and typed " package util; " at the top of each one of the classes code like this:
package util;
public class Employee
然后我写道:
import util.*;
import javax.swing.JOptionPane;
public class unit10Assignment
在程序之上。但是当我编译它时,它会告诉我。谁知道为什么?我试着玩它,当我输入import java.util *时它就消失了;相反,但我不确定我的老师想要什么,因为她的例子没有前面的java。
On top of the program. However when I compile it, it tells me. Anyone know why? I tried playing around with it and it disappeared when I typed in import java.util*; instead but I'm not sure that what my teacher wanted as her example did not have the java in front.
它还说糟糕的源文件包没有包含类Employee但是,在我输入package语句之前,所有编译和运行都很完美,而且从那以后我没有对代码进行任何更改。如果我从员工类中删除了package语句,则会出现相同的消息但是它会说另一个类不存在。
It also says " bad source file" "package does not contain class Employee " However, everything compiled and ran perfectly before I typed in the package statement and I have not made any change to the code since then. If I removed the package statement from the employee class tho, the same message would appear but it would say another class does not exist.
感谢您的帮助
注意:无论我是否放入java.util或者只是util,这个问题都会出现这个问题。
Thanks for any help Note: whether or not i put java.util or just util, this problem with the bad source still appears.
感谢您的帮助
推荐答案
我打算假设你你的项目设置如下:
I'm going to make the assumption that you have your project set up like this:
util/
Employee.java
unit10Assignment.java
bin/
(如果不是,那很好 - 只要他们在某些文件夹。 bin /
应该存在。)
(If it isn't, that's fine - so long as they're in some folder. bin/
should exist, though.)
包的方式工作是它们是硬盘驱动器上的文件夹 - 要导入的软件包要求您要导入的文件夹和类都存在于该特定文件夹中的 。这就是为什么包很方便的原因 - 你可以有两个名为 Employee
的类,让它们存在于完全不同的位置。 *
The way that packages work is that they're folders on the hard drive - the package you want to import requires that the folder and class you wish to import both exist in that specific folder. This is why packages are handy - you can have two classes named Employee
and have them live in completely different locations.*
以下是在不使用IDE的情况下将这些编译成类似于包的结构的方法。将$ HOME替换为Java类文件夹的完整路径。
Here's how you compile these into a package-like structure without the use of an IDE. Substitute $HOME for the full path of your Java class folder.
javac -sourcepath $HOME/util -d $HOME/bin *.java
以下是您运行主类的方法:
And here's how you run your main class:
java -cp $HOME/bin util.$MAIN_CLASS
这些标志的含义明细:
-
-sourcepath
指示javac在此特定目录中查找源文件。 -
-d
指定.class文件的输出目录。 / li>
-
-cp
指示java将此文件夹添加到其类路径中。
-sourcepath
instructs javac to look in this specific directory for your source files.-d
specifies an output directory for your .class files.-cp
instructs java to add this folder to its classpath.
*:实际上,非常大的项目通常可以使用与其他类相同的名称;如果您想使用特定的一个,则必须使用完全限定的类名。
这篇关于java包不存在和坏的源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!