importing from no-name package
Hi,
I am developing a s/w with a well defined package structure. But I need to use someone else's classes which have been "packaged" into a jar file.
The problem is that their classes do not have a package name; so I cant use the import statement.
If I dont use package names in my classes, then I can use their jar file without any problem. As soon as I put my files in package, I am no longer able to access their class.
Basically, is there a way to access classes that dont belong to a package from a class that belongs to a package?
thanks
Giri
It is not possible since 1.4.
The following compiles with 1.3 but with 1.4 no longer:
C:\source\java\Markov>javac13 -d . a.java b.java
C:\source\java\Markov>C:\langs\java\jdk1.3.1_12\bin\javac -d . a.java b.java
C:\source\java\Markov>javac -d . a.java b.java
C:\source\java\Markov>C:\langs\java\j2sdk1.4.2\bin\javac.exe -d . a.java b.java
a.java:2: '.' expected
import b;
^
1 error
//a.java
package pack;
import b;
public class a {
public static void main(String[] args) {
b.main(args);
}
}
//b.java
import java.io.*;
public class b {
public static void main(String[] args) {
try{
OutputStream os = new FileOutputStream ( "readonly.out");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Yes, the classes in the jar file are public and I have put the jar file on the class path. And it works fine If my new classes dont belong to a package. But as soon as I put
Package x;
as the first line of my class files, my class files no longer pick up those classes in the jar file.
And based on what I read and heard, this is a "problem" since 1.4.
I was wondering if there is a work-around to solve this problem.
thanks
> And based on what I read and heard, this is a
> "problem" since 1.4.
>
Sun and others believe the problem was in 1.3 allowing you to import from the unnamed package.
> I was wondering if there is a work-around to solve
> this problem.
>
If you can, use 1.3.
If you must use 1.4 for the final code and do not have any way to use 1.3, the only way is to go back to the owner of the jar and ask them to modify it.
If you must use 1.4 for the final code but you have access to 1.3, it should be possible to create wrapper classes for the jar'd classes and compile using 1.3, with the wrapper classes in a named package. Then you can use the wrapper classes with 1.4.
> I think I could use the last idea you suggested. But
> I am not sure how to put a wrapper class.
> Say my jar file contains.
> x.class
> y.class
> z.class
>
So if I understand correctly, these classes were compiled with no package name and you do not have access to the source code. These classes are not in a named package.
You create classes x1.class, y1.class, z1.class that look like thispackage package1;
import x;
public class x1 extends x {
Next, compile this code using a 1.3 compiler. This gives you the exact functionality of the x class but is in a named package. Then you can jar the package1.* classes and use them with a 1.4 compiler.