• Post Reply Bookmark Topic Watch Topic
  • New Topic

Running a jar file with dependencies  RSS feed

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a jar file that has a main class along with some libraries in the lib filder. When I tried to run this jar file from the command line, it fails with a class not found exception. How can I specify that it has to look for the jar files inside the lib folder inside the jar file? Is there a way to do this?

Here is what I do on the command line at the location where the jar file is:


Any thoughts?


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:How can I specify that it has to look for the jar files inside the lib folder inside the jar file? Is there a way to do this?

No, Java does not support nested jar files. There's no way to tell it to look for jar files inside a jar file.

An easy solution is to create a bat file, for example run.bat, that contains the command you are typing. Then you just have to type "run" instead of "java -classpath ..."
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Joe Harry wrote:How can I specify that it has to look for the jar files inside the lib folder inside the jar file? Is there a way to do this?


An easy solution is to create a bat file, for example run.bat, that contains the command you are typing. Then you just have to type "run" instead of "java -classpath ..."


What is that? Could you please elaborate a bit? I mean would it make any difference by calling the command (in my above post) directly or calling it from a batch file? I did not understand it!
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even this command below fails!



Saying that it cannot find a certain class. But this class is available as a library inside the lib folder which is inside the jar archive.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
545
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you on Windows or UNIX? On Windows a .bat file (or on UNIX, a .sh file), lets you run commands. You put the commands you want to run in that file so you don't have to type them in each time.

In your example, it lets you provide a zip file with all your dependencies and this batch file. Then the user just has to click run.bat rather than knowing about typing in the dependencies.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Are you on Windows or UNIX? On Windows a .bat file (or on UNIX, a .sh file), lets you run commands. You put the commands you want to run in that file so you don't have to type them in each time.

In your example, it lets you provide a zip file with all your dependencies and this batch file. Then the user just has to click run.bat rather than knowing about typing in the dependencies.


I understand that. But that is not the problem. The command actually fails with a ClassNotFoundException. How to get rid of that. The actual class that it says it cannot find is actually available as a jar file inside the lib folder. This lib folder is inside the jar file that I'm trying to run!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
545
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Jesper said, you can't. I'm suggesting you unzip that jar file. Then you will have a lib folder on the actual file system.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:As Jesper said, you can't. I'm suggesting you unzip that jar file. Then you will have a lib folder on the actual file system.


Thanks for that. I just realized by myself that it is not possible. The only option that I have now is to either instruct maven to run it when I build the project or like you said, unzip the jar file and then run it.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:What is that? Could you please elaborate a bit? I mean would it make any difference by calling the command (in my above post) directly or calling it from a batch file? I did not understand it!

The difference is that you only have to type "run<Enter>" to run it instead of typing that whole long command line every time you want to run it...
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Joe Harry wrote:What is that? Could you please elaborate a bit? I mean would it make any difference by calling the command (in my above post) directly or calling it from a batch file? I did not understand it!

The difference is that you only have to type "run<Enter>" to run it instead of typing that whole long command line every time you want to run it...


Please don't get me wrong. I'm very much aware of batch files and I have written dozens and dozens of them for other purposes. But that was not the point of my original post to ask how to write a batch file, rather to find if I could run a jar file with self contained dependencies.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!