11 Replies Latest reply: Mar 30, 2012 2:36 PM by gimbal2 RSS

    Reference to a class inside a jar which is inside a jar

    919944
      Hi there,

      I run into this problem:
      - My app is packaged in a jar file, app.jar
      - Inside app.jar, there is a log4j's jar file, log4j.jar.
      - When the app is starting, JVM complaints that it could not find a log4j class. I guess the app could not find the class inside log4j.jar which is inside myapp.jar.

      So my question is, is there any ways/configurations to make reference to classes in inside a jar which is also inside a jar?
      Thanks in advance.
        • 1. Re: Reference to a class inside a jar which is inside a jar
          gimbal2
          Inside app.jar, there is a log4j's jar file, log4j.jar.
          So where did you read that this was possible?
          • 2. Re: Reference to a class inside a jar which is inside a jar
            919944
            gimbal2 wrote:
            So where did you read that this was possible?
            I didn't read anywhere about that. I have just encountered this kind of issue. That isn't possible, is that?
            • 3. Re: Reference to a class inside a jar which is inside a jar
              gimbal2
              Bob Sadarka wrote:
              gimbal2 wrote:
              So where did you read that this was possible?
              I didn't read anywhere about that.
              So you were just trying stuff eh. Yeah, that never works when you're dealing with complicated material such as a complete development platform.

              For future reference, I would stick to the "new to java" forum for now, when you do people will be more lenient to the 'mistakes of the novice' to put it as friendly as I can possibly make it. But we're here now so lets roll with it: what you are experiencing is your first contact with the enemy that you should treat as your friend: the classpath. It is an important concept that you should study in-depth because your entire experience with Java hinges on your properly understanding not only how it works, but also WHY it is necessary. There are many articles on the internet that discuss it, a search for "java classpath" will probably already get you enough of them.


              EDIT: I forgot to mention that of course everything is possible including getting a jar to be loaded from a jar, but that requires third party libraries/tools.
              • 4. Re: Reference to a class inside a jar which is inside a jar
                919944
                Hi gimbal2,

                Thank you for your suggestion. I do known something about the classpath before. Maybe I didn't explain my problem well enough. Let me try to explain: There is no problem if the log4j.jar is outside of the app.jar. However the problem is that I want to distribute only one single jar file for may app, so that I have put log4j.jar into the app.jar, and the problem comes from here.

                Edited by: Bob Sadarka on Mar 23, 2012 2:14 AM
                • 5. Re: Reference to a class inside a jar which is inside a jar
                  DrClap
                  Bob Sadarka wrote:
                  However the problem is that I want to distribute only one single jar file for may app
                  Yes. Lots of people have that problem. Usually they fix it by not wanting to do that any more. Why did you invent that requirement?
                  • 6. Re: Reference to a class inside a jar which is inside a jar
                    919944
                    DrClap wrote:
                    Bob Sadarka wrote:
                    However the problem is that I want to distribute only one single jar file for may app
                    Yes. Lots of people have that problem. Usually they fix it by not wanting to do that any more. Why did you invent that requirement?
                    Because I want to distribute a single jar file to my client (zip distribution is considered but the client has to extract the file), so the client can execute the program immediately. Anyway, I have solved my problem by using Java logging API instead log4j, though java logging is not as good as log4j.
                    • 7. Re: Reference to a class inside a jar which is inside a jar
                      EJP
                      It's a lot better than log4j in one way: it's already in the JDK and doesn't require you to ship extra JARs. It also doesn't require you to keep up to date with third parties.
                      • 8. Re: Reference to a class inside a jar which is inside a jar
                        gimbal2
                        EJP wrote:
                        It's a lot better than log4j in one way: it's already in the JDK and doesn't require you to ship extra JARs. It also doesn't require you to keep up to date with third parties.
                        And apparently it creates less overhead as well if I have to believe the engineers at JBoss; when you have complex systems that do a lot of logging (which all big systems should be doing IMO) it can actually make a big difference on the runtime performance.
                        • 9. Re: Reference to a class inside a jar which is inside a jar
                          919944
                          So, do you guys mean that JDK logging is better than log4j in term of runtime performance for big apps? However I found log4j is more flexible.
                          • 10. Re: Reference to a class inside a jar which is inside a jar
                            DrClap
                            Bob Sadarka wrote:
                            Because I want to distribute a single jar file to my client (zip distribution is considered but the client has to extract the file), so the client can execute the program immediately. Anyway, I have solved my problem by using Java logging API instead log4j, though java logging is not as good as log4j.
                            I'm surprised that you let details like how to distribute the application dictate its fundamental architecture. And because you haven't figured out how to distribute it properly, you ended up with an application which doesn't measure up.

                            If you had simply used Java Web Start to distribute the application, you wouldn't have been constrained by that single-jar rule. But I assume you didn't know about Web Start. So you asked the wrong question.

                            Your question was "How can I conveniently distribute an application which requires several jar files?" But you didn't ask that one. Instead you chose a solution (the wrong one), which was to try and use only one jar file. But this led you into problems. So instead of rejecting the solution and looking for a better one, you decided to ask a question about your failed solution. And having only got non-responsive answers (because it was the wrong question) you came up with a poor answer to your failed solution.

                            This happens a lot in problem-solving and you aren't the first one to try building a boat while halfway across a swamp. I mention it just to suggest that the next time you find yourself in the swamp, you should consider questioning the decision that made you jump into the swamp.
                            • 11. Re: Reference to a class inside a jar which is inside a jar
                              gimbal2
                              DrClap wrote:
                              Your question was "How can I conveniently distribute an application which requires several jar files?" But you didn't ask that one. Instead you chose a solution (the wrong one), which was to try and use only one jar file. But this led you into problems. So instead of rejecting the solution and looking for a better one, you decided to ask a question about your failed solution. And having only got non-responsive answers (because it was the wrong question) you came up with a poor answer to your failed solution.

                              This happens a lot in problem-solving and you aren't the first one to try building a boat while halfway across a swamp. I mention it just to suggest that the next time you find yourself in the swamp, you should consider questioning the decision that made you jump into the swamp.
                              Hm, nice summary of a very common occurrence.

                              As for "distributing as one jar", with plain old Java it isn't possible but that of course doesn't stop people from finding ways to do it anyway. This is one such solutions:

                              http://www.jdotsoft.com/JarClassLoader.php