所以我创建了一个名为 util 的文件夹,并在其中放置了四个类以及名为 unit10Assignment 的程序.我创建了一个 package 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 类" 但是,在我输入包语句之前,所有的东西都编译并运行得很好,从那以后我没有对代码进行任何更改.如果我从员工类中删除了包语句,则会出现相同的消息,但它会说另一个类不存在.
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/
(如果不是,那很好 - 只要它们在 some 文件夹中.但 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 文件的输出目录.-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包不存在且源文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!