不要包必须匹配java文件所在的子目录吗?(Don't packages have to match the subdirectories the java file is in?)
今天早上我正在为我的java认证编写一些练习程序,并且注意到我错误地输入了一个包名称,所以它与java文件所在的子目录不匹配。我编译了期望出现错误的代码,但编译的所有文件 - - 甚至没有警告。
我搜索了一下,我读的大部分网页都说包名必须与子目录相匹配。 我的经验表明情况并非如此。
当我试图运行该程序时,它不起作用,因为.class文件位于错误的目录中。 我把它移到了正确的目录下,并得到这个错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sample/directory /doesnt/even/exist/OtherPackageMemberModifiers (wrong name: com/sample/chap01/O therPackageMemberModifiers)
所以我认为我看到的是如果程序包和子目录不匹配,Java代码将被编译 ,但如果你这样做,似乎没有办法运行代码。 那是对的吗?
I was writing some practice programs for my java certification this morning, and noticed that I had mistyped a package name, so it didn't match the subdirectory the java file was in. I compiled the code expecting an error, but everything compiled file -- not even a warning.
I googled around a bit, and most of the pages I read said that the package name had to match the subdirectory. My experience shows that's not the case.
When I attempted to run the program, it didn't work because the .class file was in the wrong directory. I moved it to the correct directory, and got this error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sample/directory /doesnt/even/exist/OtherPackageMemberModifiers (wrong name: com/sample/chap01/O therPackageMemberModifiers)
So what I think I'm seeing is that Java code will compile if the package and the subdirectory don't match up, but there doesn't seem to be a way to run the code if you do that. Is that correct?
最满意答案
软件包名称必须与目录名称匹配才能正确找到类文件。 它不必在编译时与某些编译器(例如javac)匹配目录名称,但其他(如Eclipse)至少会发出警告。
如果你这样做的话,“运行代码的方式”是创建目录结构并手动将其放在那里 - 类文件本身是完全有效的。
请注意,如果使用
-d
标志,则无论源位置如何,javac都会为您构建适当的目录层次结构。 例如:javac -d bin ClassInPackage.java
将在
bin
下创建任何所需的目录以匹配ClassInPackage.java
声明的包。说完所有这些之后,我仍然强烈建议你让源目录与包匹配,即使你没有它也可以逃脱:)
The package name has to match the directory name in order for the class file to be found correctly. It doesn't have to match the directory name at compilation time for some compilers (e.g. javac) although others (such as Eclipse) will at least give a warning.
The "way to run the code if you do that" is to create the directory structure and put it in there manually - the class file itself is entirely valid.
Note that if you use the
-d
flag, javac will build the appropriate directory hierarchy for you, regardless of source location. For example:javac -d bin ClassInPackage.java
will create any required directories under
bin
to match the package declared inClassInPackage.java
.Having said all of thise, I'd still strongly encourage you to make the source directories match the packages, even though you can get away without it :)
相关问答
更多-
不要包必须匹配java文件所在的子目录吗?(Don't packages have to match the subdirectories the java file is in?)[2023-02-03]
软件包名称必须与目录名称匹配才能正确找到类文件。 它不必在编译时与某些编译器(例如javac)匹配目录名称,但其他(如Eclipse)至少会发出警告。 如果你这样做的话,“运行代码的方式”是创建目录结构并手动将其放在那里 - 类文件本身是完全有效的。 请注意,如果使用-d标志,则无论源位置如何,javac都会为您构建适当的目录层次结构。 例如: javac -d bin ClassInPackage.java 将在bin下创建任何所需的目录以匹配ClassInPackage.java声明的包。 说完所有这 ... -
您可以使用File类列出目录。 File file = new File("/path/to/directory"); String[] directories = file.list(new FilenameFilter() { @Override public boolean accept(File current, String name) { return new File(current, name).isDirectory(); } }); System.out.printl ...
-
如何用多个相互依赖的子目录建立一个C ++项目?(How can I build a C++ project with multiple interdependent subdirectories?)[2022-07-31]
由于您的项目中的目录结构只是为了保持您的文件的组织,一种方法是使用一个CMakeLists.txt自动查找src目录中的所有源文件,并将所有目录添加为其中包含头文件的包含目录。 以下CMake文件可以作为起点: project (Foo) file(GLOB_RECURSE Foo_SOURCES "src/*.cpp") file(GLOB_RECURSE Foo_HEADERS "src/*.h") set (Foo_INCLUDE_DIRS "") foreach (_headerFile ${F ... -
我不清楚你想要实现的目标。 但在我看来,您希望扫描.xml文件的文件夹,并从所有这些.xml文件中获取特定元素并将其存储到列表中。 下面是一个示例程序,它扫描文件夹中的.xml文件并将所有“Element:path”字符串存储到列表中。 您可以使用StringTokenizer来分隔它们。 同样使用StringTokenizer,您可以将文件名与文件路径分开。 import java.io.File; import java.util.ArrayList; import java.util.List; i ...
-
当然。 这真的不是一个编程问题,但... find . -regex '.*regex_here.*' 看看man find 。 Sure. This really isn't a programming question, though... find . -regex '.*regex_here.*' Have a look at man find.
-
将你的循环改为: for textfile in findfiles(r'C:\\Top', '*.txt'): if not textfile.startswith(r'C:\\Top\\1'): filename = os.path.basename(textfile) print filename Change your loop to this: for textfile in findfiles(r'C:\\Top', '*.txt'): if n ...
-
应该可以编写一个类似这样工作的类加载器,甚至可以通过子类化URLClassLoader 。 It should be possible to write a classloader that works like this, perhaps even by subclassing URLClassLoader.
-
您的要求是输入文件夹然后显示文件夹的所有文件(包括文件夹的文件)的大小。 我们定义文件夹的大小是文件夹(包括子文件夹)中文件大小的总和。 所以,源代码的过程如下: (1)列出文件夹的所有文件作为输入。 (2)计算(1)中列出的文件大小。 (3)显示文件类型(FILE或DIR),(1)中列出的文件路径和(2)中计算的文件大小。 (1)和(3)的源代码如下: public static void showFileSizes(File dir) { File[] files = dir.listFiles() ...
-
其实我推荐你: 1)重新考虑你的路由 2)为管理面板布局创建/layouts/admin 3)创建/admin/posts/edit视图以进行编辑 但对于你的问题,这里是一个简单的例子: router.get('/post/:slug/edit', (req, res) => { Post .findOne({slug: req.params.slug}) .exec((err, post) => { res.render('post', {includeEdit: tr ...
-
将特定子目录中的某些文件移动到新目录中(Move certain files from specific subdirectories into a new directory)[2022-07-27]
假设你的CSV文件如下所示: George,/home/geo/mtvernon.java,Betsy Abe,/home/honest/gettys.java,Mary 您可以使用以下shell命令移动文件: $ cut -d, -f2 < x.csv | xargs -I '{}' mv '{}' /home/me/new-directory Assuming that your CSV file looks like this: George,/home/geo/mtvernon.java,Be ...