For a project I have used two .jar files which are in its lib. However now I wish to use this project as a lib. However when I export my project to a .jar it no longer works due to the missing other two (as far as I can tell). I am quite familiar with java as a language, however not as a system. Could someone help me set up the dependencies for my project or somehow incorporate them? I do not want to manually install the dependencies. (I am using jdk17 on eclipse). I have no idea if what I'm trying to do is possible and I'm lost at this point.
#Creating a Jar containing it's own dependencies.
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
jar file, the dependencies are not included in the jar file. To create a jar file that includes its own dependencies, you can use one of the following approaches:
-
Using a build tool like Maven or Gradle: If you are using a build tool like Maven or Gradle, you can configure it to create an executable jar file with all its dependencies included. These build tools have plugins (e.g., Maven Shade Plugin or Gradle Shadow Plugin) that can handle this automatically. You just need to configure the plugin in your build configuration file (pom.xml for Maven or build.gradle for Gradle) and run the appropriate command to create the jar file.
-
Using an IDE: Most IDEs provide options to export a project as a runnable jar file with dependencies included. For example, in Eclipse, you can right-click on your project, select "Export", choose "Runnable JAR File" under "Java", and then select the option "Package required libraries into generated JAR". This will create a jar file with all its dependencies included.
-
Manually creating a fat/uber jar: If you don't want to use a build tool or IDE-specific options, you can manually create a fat/uber jar by extracting all the classes from your project's dependencies and packaging them along with your project's classes into a single jar file. You can use tools like Apache Maven Shade Plugin or Apache Ant Jar task to achieve this.
Here is an example of how you can use Apache Maven Shade Plugin to create an executable jar with all dependencies included:
- Add the following configuration to your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
- Run the following command to create the executable jar file:
mvn package
This will create a jar file in the target directory with all its dependencies included.
Note: When using any of these approaches, make sure that you have the necessary licenses and permissions to distribute the dependencies along with your project.
My project does not include a main function so the runnable jar (which contains the option to package required libraries) is not an option I can take.
As for the maven route, this seems like a viable option. However I have no clue how I would add the jar to the dependencies as it is a library we have a licence for and such. I do not think it is in the list that maven has.
two options
well, three
- Have the jars for your dependencies on the side
so get all your dependencies with mvn dependency:copy-dependencies and then have them on the classpath/modulepath when you launch the program
java --class-path deps -jar main.jar
- Bundle them into an uberjar
pro: easier to deploy
con: janky
we did 2 for work for a long time, but nowadays I think i'd prefer 1
- make a jlinked image
this is the stranger one of the list, but the one sanctioned by the jdk
depending on your dependencies you can zip your project up into a modular runtime image
basically just a version of java that contains only what you need for your app
there is a 3.5 and so on for using jpackage and whatnot
but with just 3 you need every dependency to have a module-info.class file in it
However now I wish to use this project as a lib
Oh i might have misread. you mean you want to use the project as a library from another project?
Yeah
and you have a library which is not open source, but just a jar you downloaded from the internet?
and you paid for a license for
Long story short, its PLCCom and postgresql. PLCCom is something we pay for (as far as i understood) and the other is just for reading from postgres.
I wrote a bunch of methods that connect and send stuff to plc's
gotcha
and i want to use that in another project
and are you using maven or just the "eclipse IDE build stuff"
are you in an organization with more code infrastructure or is that basically how everyone works?
this is the first project we are actually making in java. The rest is anchient bbj shit we need to replace at some point but thats not important
bbj?
fascinating
but that wont touch this so dw bout it
okay well let me talk first about how the "maven system" works usually
please
so maven is two things
first is a build tool. it runs code that compiles, packages, etc. your code
second is a dependency resolver
similar to npm then?
in your pom.xml file - Project Object Model - you declare a list of dependencies for your "maven module"
somewhat, yes
so dependency declarations have a few required parts
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>dev.mccue</groupId>
<artifactId>json</artifactId>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
the "group", the "artifact", and the "version"
and optionally the "scope"
the scope comes into play with the build tool part of things
it says when the dependency should be available
so when I personally publish a library to "maven central" - the repo that everyone uses for open source stuff
I prove that I own mccue.dev via DNS stuff, then I can publish under the dev.mccue group whatever artifacts I want
so in this case I depend on com.fasterxml.jackson.core/jackson-databind
when maven sees this, it will check the "proper" place in the repo
and it will look in the pom.xml that is there
and it will get any dependencies of jackson-databind
so on and so forth until it has one version of every dependency
Ok so far i can understand (besides the dev.mccue part but im sure i dont need to host stuff)
well thats basically just like, only google can publish under com.google
and thats a rule for the public repos
understood
$ jresolve --print-tree pkg:maven/com.fasterxml.jackson.core/[email protected]
com.fasterxml.jackson.core/jackson-databind 2.15.2
. com.fasterxml.jackson.core/jackson-annotations 2.15.2
. com.fasterxml.jackson.core/jackson-core 2.15.2
so maven provides a pretty strong notion of a dependency
you depend on one thing and automatically get everything that thing depends on
so if you are gonna be making more and more software in Java, it probably makes sense to invest in that system
the problem is that you don't have a repository you can find your PLCCom dependency in
its just a jar you got
and maven doesn't have support for "i depend on this jar lying about"
i did try to mvn install of some kind
so, you kinda need to have your own repository
where i had to put in the path to the jar for the local repository
a repository can be local, just a directory structure on your computer
but really you want it to be shared in some way
so running your own maven repo / paying for one / etc. might be the move (though maybe not idk)
another another option is to not bother with that and do your entire company's codebase in one maven project
as we already have some hosting here we might be able to get the local repo. But i can figure that out with my superiors later.
you need to go deeper, but you can deal with it that way
something like this^
and for context, I wrote this
so uhh, if you need help gimmie some $$ and i'll fly out
but thats the general shape of your problem
java is fine with just a bunch of deps in a folder. maven is not
and maven is the way that you can have "transitive dependencies" in a sane-enough way
thats a deployment strategy
not really viable for a library
darn
there are languages on the JVM with built-in support for local deps (like clojure)
but thats a story for another day
you can make an uberjar if you can safely shade your dependencies
but you can't just keep doing that forever
it basically only works once
then i might just stick to the option 1 strat and have a folder. The bossman is not so much for going to public domains with our propiatary stuff
well again, thats a deployment strat
i was answering the wrong question
The basic problem i had was just management of the dependencies and you answered that (very well might i add)
Im simply out of my depth with this stuff but this has given me a clear view of my options
May i thank you for your time and explaination
Remind me later in the day and I can make an example project for you
I can also talk some of it through
Maybe some other time then? I dont know what time it is for you but its 17:45 here and im gonna clock out soon.
I am in EST, so it is 11:47
For now we will be keeping it extra in the folders then (option 1). But if we drastically scale things up here, we might consider hosting our own maven repo.
(+ having a multi-module maven build)
(+ ...)
yeah i'd be down to talk this through more - its an interesting situation for me
For now ill just leave it at this, and call the question closed. We could perhaps have a chat about it tomorrow if my timetable will allow for it (and yours).
👍
Once again many thanks for the help chief, much apreciated.
@fast anchor
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure 👍