首页 \ 问答 \ 不要包必须匹配java文件所在的子目录吗?(Don't packages have to match the subdirectories the java file is in?)

不要包必须匹配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?

更新时间:2023-02-03 17:02

最满意答案

软件包名称必须与目录名称匹配才能正确找到文件。 它不必在编译时与某些编译器(例如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 in ClassInPackage.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 :)

相关问答

更多

相关文章

更多

最新问答

更多
  • 在csproj中使用appdata环境变量(Use appdata environment variable in csproj)
  • 从背景返回后,Skobbler Map崩溃(Skobbler Map crashes after returning from background)
  • 如何保持对绑定服务的轮询?(How to keep polling a bound service?)
  • ASP.NET单选按钮jQuery处理(ASP.NET radio button jQuery handling)
  • Linux上的FORTRAN图形库(FORTRAN graphic library on Linux)
  • 我们如何根据索引更新dynamodb表(不基于primary has和range key)(how can we update dynamodb table based on index(not based on primary has and range key))
  • 功能包装避免重复(wrap of functions avoid duplicating)
  • Android BroadcastReceiver和Activity.onPause()(Android BroadcastReceiver and Activity.onPause())
  • 无法使用phonegap 2.4在Android上播放录音(unable to play audio recordings on android using phonegap 2.4)
  • VS2015 + Resharper:不要使用C#6(VS2015 + Resharper: Don't use C#6)
  • 大学电脑四级对初学者来说要多久能过
  • 特殊字符删除?(Special characters remove?)
  • Android视频教程现在网上的都比较零散呢?有些太坑爹了,感觉老师就是在想当然的讲
  • 计算同一个表中不同行之间的差异[重复](Calculate delta's between different rows in same table [duplicate])
  • Javaweb开发,技术路线是什么?该怎么写?
  • JavaScript只在php代码中执行一次(JavaScript only executes once inside php code)
  • 不兼容的字符编码:ASCII-8BIT和UTF-8(incompatible character encodings: ASCII-8BIT and UTF-8)
  • Clojure(加载文件)给出错误(Clojure (load-file) gives an error)
  • 为具有瞬态scala依赖性的spring-xd项目优化gradle(Optimize gradle for spring-xd project with transient scala dependency)
  • 如何才能在Alpha测试模式下发布我的应用程序?(How can I publish my app in Alpha test mode only?)
  • “没有为此目标安装系统映像”Xamarin AVD Manager(“No system images installed for this target” Xamarin AVD Manager)
  • maven中的Scalatest:JUnit结果(Scalatest in maven: JUnit results)
  • 使用android SDK将文件直接上传到存储桶中的文件夹(Upload a file directly to a folder in bucket using android SDK)
  • 是否应将plists导入CoreData?(Should plists be imported to CoreData?)
  • java.lang.reflect.InvocationTargetException JavaFX TableView(java.lang.reflect.InvocationTargetException JavaFX TableView)
  • 根据唯一列值动态创建多个子集(Dynamically create multiple subsets based on unique column values)
  • 使用CSS可以使HTML锚标签不可点击/可链接吗?(Is it possible to make an HTML anchor tag not clickable/linkable using CSS?)
  • 嵌套的模板可能性(Nested template possibilities)
  • 任何方式在iOS7 +上以编程方式打开蓝牙(Any way to turn on bluetooth programmatically on iOS7+)
  • 如何为给定的SQL查询编写JPA查询(How I can write JPA query for given SQL query)