• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compile error:It is indirectly referenced > from required .class files

 
bill zhang
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't get my application compiled.
1. I created an application called second.jar which imports first.jar. There is a method in second jar throws an exception implemented in first.jar.
2. And then I created another application called third.jar which imports second.jar.
3. There is a method in third.jar uses the method in second jar. Therefore, I need to declare the exception in first.jar in third.jar.
4. Error: compiler complains on the exception in first.jar 'The type ....Exception cannot be resolved. It is indirectly referenced > from required .class files'
Can someone give me any clue? Thanks.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Which compiler are you using?
You would need to import the classes in each .jar directly. A class in jar2 uses a class in jar1, and a class in jar3 uses that same class in jar2. You have to import it from jar1 into jar3.
Have you set up a classpath? You would need to use the -cp options for the java and javac tools.

Not sure what else to suggest. Sorry. But this question is too difficult for beginners. Moving it.
 
bill zhang
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.
OK. I'll move the post to one level higher.
Since second.jar contains first.jar and I imported second.jar into third.jar, there ought to be a way that we can make first.jar visable in third.jar. I just wanna try to avoid importing first.jar to third.jar directly.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,

Jars can neither contain nor import other jars, so we actually can't understand what you mean. Can you talk in terms of classes and files? Classes can use other classes, so a class in one jar could depend on a class in another jar, and so both jars would be required at runtime. But you can't pack one jar file inside another (if that's what you're saying.) You need to have all the jars available directly on the file system. It's possible for one jar's Class-Path attribute to mention another jar file, but again, that second jar must be available as a file, not inside the first jar.

When you run an application that needs multiple jars, you can just specify all the jars on the command line:

java -cp a.jar;b.jar;c.jar com.me.MyClass

 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Bill,

If what Campbell understood of your post is correct, then it's ok else I didn't get what you mean by importing jars. If importing classes, you will have to import the classes based on the need.

If you do not want to import classes from jar 1 into jar 3 because you already imported them in classes of jar 2, then, sorry - can't be done!

If your classes in jar 1 or 2 are made such that the objects encapsulate those classes, then maybe.

Regards
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:
When you run an application that needs multiple jars, you can just specify all the jars on the command line:

java -cp a.jar;b.jar;c.jar com.me.MyClass



Just to add, in case you are on UNIX:


 
bill zhang
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the confusion. I am using RAD. So I just used 'import' in my previous posts. What I did in RAD was: 1. I created First project and then export it as first.jar. 2. created Second project, and import first.jar into the project. When I export this project to second.jar, first.jar is included in second.jar automatically. So this is actually a wrong practice according to the previous posts, correct?
Thanks.
 
Anita Karacs
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear bill zhang,

Some years passed since you faced this, and now I faced this issue too. How did you manage to solve this? I want to avoid importing first.jar to third.jar directly, too, but now I can't see any other solution, and I need that exception. I think the third program should only import second.jar, otherwise it is pointless. To make it clear I want to create my own mail sender API, and the third program requires the MessagingException, but I don't want to import mail.jar into my program.

Thanks for the replies!
 
Winston Gutkowski
Bartender
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anita Karacs wrote:To make it clear I want to create my own mail sender API, and the third program requires the MessagingException, but I don't want to import mail.jar into my program.

Er...why not?

If that's where MessagingException is, I don't think you have much choice.

My advice: Back up and explain the problem, not the solution.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic