开发者

Is there any way to force Java to recognize the unnamed package from a named package?

I开发者_Go百科n order to complete a project, I need to alter a class file in a named package.

I cannot put it into the unnamed package; it is imported itself from other classes.

I cannot put all the dependencies into named packages, they are also relied upon by other classes.

My goal is to force javac through whatever means necessary to allow imports from the unnamed package. It's bad practice, but I have no other choice.

Alternatively, how would I go about putting all of the imported classes into a package without recompiling them, reference that package in order to compile the target class, and then remove those references after the compile?


I solved the problem. I compiled the program by making barebones definitions of all classes and methods it imported inside a named package, and then used a program called Classeditor to edit out the package names/path after compiling.


how would I go about putting all of the imported classes into a package WITHOUT recompiling them

You can't, the package name is part of the full class name and present in the class-file itself.

You can use reflection to access these classes, this is a ugly solution for a ugly problem.


I believe that it was possible to import classes from the unnamed package into classes in named packages until java 1.4. However, that does not work anymore.

That is, if you have two classes like this:

File A.java
import b.B; // <-- This is ok
class A {
...
}

File B.java
package b;
import A; // <-- Does not compile after 1.4
class B {
...
}

then it would be possible to reference class B from class A but not the other way. You cannot import classes from the unnamed package.

You may try to compile your classes using JDK 1.3. They will still run in JDK 1.4 and above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜