This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes jar files and import statement question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "jar files and import statement question" Watch "jar files and import statement question" New topic
Author

jar files and import statement question

Josefina Smith
Greenhorn

Joined: Aug 03, 2013
Posts: 5
In my code I use a line like the following where I import a class file:

[code]
import com.mypackage.test.api.abc.MyClassName;
[/code]


this was in a jar file included in my build at compile time.

first.jar - com - mypackage - test - api - abc - MyClassName


this jar file has changed and the now I have to import my class from a jar file within this jar file.

first.jar - com - mypackage - test - api - abc -
- second.jar - com - mypackage - test - api - abc - MyClassName


how would one write the import statement in this case?


Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Did it give you an error when you tried to use the same import statement?


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Josefina Smith
Greenhorn

Joined: Aug 03, 2013
Posts: 5
it only gave the typical unable to resolve class error.

I was think that I must be forgetting how to load a class in this case.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Josefina Smith wrote:how would one write the import statement in this case?

The import statement won't change. It's your classpath that needs to change - you need to include second.jar in it.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Josefina Smith wrote:this jar file has changed and the now I have to import my class from a jar file within this jar file.


Jar files within jar files can't be used as part of the classpath. At least, not without a lot of work -- I vaguely recall reading about a product which allowed you to use a specialized classloader to do that, but I really wouldn't recommend going down that road unless you're being forced to use a jar within a jar by some inflexible manager out of a Dilbert comic. It's better to just not do that.
Josefina Smith
Greenhorn

Joined: Aug 03, 2013
Posts: 5
Thanks for the tips guys! What I ended up doing is pulling my inner jar out next to my outer jar and then my compiler could easily read it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jar files and import statement question
 
Similar Threads
compile error:It is indirectly referenced > from required .class files
Problem when creating / running a jar file
basic packages question
creating a package of classes
Tomcat 4.0 and JSP- problem using beans