| Author |
Import a non-packaged class??
|
Michael Remijan
Ranch Hand
Joined: May 29, 2002
Posts: 103
|
|
|
I have to use a jar file which contains class files which are not packaged. None of the source files have the 'package xxx.yyy.zzz;' code in them. Now I'm developing an application that is packaged. When I try to compile I get standard "cannot resolve symbol" error even though the jar file is in the classpath. So how do I import a non-packaged class so I can use it in my code?
|
<a href="http://www.linkedin.com/in/mjremijan" rel="nofollow">
<img src="http://www.linkedin.com/img/webpromo/btn_viewmy_160x25.png" border="0" >
</a>
|
 |
David Harkness
Ranch Hand
Joined: Aug 07, 2003
Posts: 1646
|
|
As far as I know and have read many times here, you can't. Though, I'm curious if perhaps doing just "import YourClass;" would work. Needless to say, declare a package in all of your classes to maintain sanity.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24040
|
|
Originally posted by David Harkness: I'm curious if perhaps doing just "import YourClass;" would work.
It used to work, but recent Javac implementations have made a specific point of not accepting this syntax. [ February 08, 2005: Message edited by: Ernest Friedman-Hill ]
|
[Jess in Action][AskingGoodQuestions]
|
 |
sandip mense
Greenhorn
Joined: Oct 14, 2003
Posts: 28
|
|
Michael, I had exact issue. If you use jdk 1.3.6 or below you will be able to refer a non packaged java file into a packaged java file. But jdk1.4 onwards this support is disbaled. I beleive sun has recognised this as bug and have disabled this. So if you want to compile this use jdk 1.3 or below. Sandip
|
 |
Jeroen Wenting
Ranch Hand
Joined: Oct 12, 2000
Posts: 5093
|
|
You may be able to create instances of these classes using reflection. Since Class.forName(String classname) takes a fully qualified classname always it should in theory be able to return a firstclass Class object for a class that has no package. Calling newInstance() on this would give you the class instance you want. Of course this only works if every class you want to create has a no argument constructor. To instantiate classes by using a constructor that takes arguments, use another method. Here you'd use Class.forName("SomeClass").getConstructor(Class[]).newInstance(Object[]) instead. Check out the relevant API docs for details.
|
42
|
 |
Michael Remijan
Ranch Hand
Joined: May 29, 2002
Posts: 103
|
|
|
Using class.forName is actually a really good idea. This is a 3rd party library so I don't have the luxury of repackaging it. What I think i'm going to do is create a wrapper class which has the method calls I need. This wrapper will then use reflection to call the matching method on the real object. I think this will work out ok.
|
 |
Jeroen Wenting
Ranch Hand
Joined: Oct 12, 2000
Posts: 5093
|
|
You may want to consider an object pooling mechanism though for your created objects. Reflection does impose a performance penalty.
|
 |
shochan vanden
Greenhorn
Joined: Feb 09, 2005
Posts: 4
|
|
Originally posted by Michael Remijan: I have to use a jar file which contains class files which are not packaged. None of the source files have the 'package xxx.yyy.zzz;' code in them. Now I'm developing an application that is packaged. When I try to compile I get standard "cannot resolve symbol" error even though the jar file is in the classpath. So how do I import a non-packaged class so I can use it in my code?
Simply put your "no-package" compiled classes in a separate jar file and include that jar in your deployment (WAR, EAR, other JAR or whatever your deployer wants). (BTW wouldn't your "no-package" classes happen to be JasperReports generated classes? In such case I could give you a patch that generates them WITH package declaration.) Cheers
|
 |
Michael Remijan
Ranch Hand
Joined: May 29, 2002
Posts: 103
|
|
Originally posted by shochan vanden: Simply put your "no-package" compiled classes in a separate jar file and include that jar in your deployment (WAR, EAR, other JAR or whatever your deployer wants). (BTW wouldn't your "no-package" classes happen to be JasperReports generated classes? In such case I could give you a patch that generates them WITH package declaration.) Cheers
No, it's not JasperReports. It's just some libraries another team developed.
|
 |
Jeroen Wenting
Ranch Hand
Joined: Oct 12, 2000
Posts: 5093
|
|
Originally posted by shochan vanden: Simply put your "no-package" compiled classes in a separate jar file and include that jar in your deployment (WAR, EAR, other JAR or whatever your deployer wants). Cheers
won't work I'm afraid. That will add them to your application but you can't call them from classes which are in a package. When calling a class without an explicit package only the current package is used to look up the class, classes without a package don't have a package name and therefore can't be accessed from classes in a package.
|
 |
 |
|
|
subject: Import a non-packaged class??
|
|
|