Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Import a non-packaged class??

 
Author
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Michael Remijan
Author
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to consider an object pooling mechanism though for your created objects.
Reflection does impose a performance penalty.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Author
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
For my next trick, I'll need the help of a tiny ad ...
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic