Compiling JAVA project with external libraries, is this possible? - java

I'm currently trying to compile my project which would include two external libraries.
json-simple.jar and mysql-connector-java-5.1.42-bin.jar
At the moment I compiling my program and the problem is that i need to include these libraries in the classpath of created compiled project, but i do need only that these libraries would inside .jar project compiled file and i can simply run .jar file and make it work withuot including classpaths or something like that.
Is that possible, don't have idea how.

Yes, it is possible.
I'll help you a bit:
How to make an executable jar file?
Basically unpack the files from the libraries into the folder with your compiled classes (you know how to compile them, right?) then prepare a MANIFEST.MF, put it the folder with all these classes, then create a jar as explained in the answers under the link (jar cfm jarexample.jar jexample.mf *.class). If you have any problems then read the documentation of the commands like jar and javac.
Have fun!

Related

Creating a JAR using Ant but not including external jars inside

I've recently come with the need to create for a Java project a build.xml file in which is meant to be run using the terminal.
The issue is that until now we've used Eclipse as the utility to run the build.xml while depending on Eclipse UI to let the Ant package manager to create our jars, but not putting the external jars in each jar we create.
That way, the memory in which the jar libraries take(~40MB) isn't replicated to each Jar we make.
I'm pretty a beginner in Ant so what I'm trying to do is create a build.xml in which take a folder of Jar libraries, and for the project which is a Jar library itself, create the Jar while knowing the libraries but not include them in the end Jar.
I alternatively tried understanding how eclipse teels the build.xml for ant but the classpath of the project, how to access the jars and other configurations, so if there's a possibility for that then it will be much better.
If possible, it would be great to help with that.
I've tried of course seeing other questions on Stackoverflow but didn't saw something similar to this.
If there's one of course I'll be happy to see it :)
Thanks heads up! :)

How to include external jar files into a java project without an IDE?

I do have a project that depends on external libraries (jfoenix is one of them). I didn't want to add the libraries using IDEs like Eclipse, Netbeans or any other IDE. The Directory structure looks like,
Project Folder:MyProject
Package :afa
Where and how should I include the external jar files and compile my classes which are inside the package? I found answers that say, put your jar files in the classpath or -classpath but I need a step by step guide on how I can do this. All my java classes are in the same package.

How to compile a Java library

I have a .jar library and used 7-zip to extract the files and edit the .java source code inside.
I need to be able to build this to update the .class files before I turn it back into a .jar.
The library won't build as it's missing dependencies, but I don't know how to add these.
The library's using JNI, and LibGDX, and as an amateur, I am out of my depth on this problem that I am stuck on.
The original .jar file is in the link below
https://github.com/finnstr/gdx-liquidfun-extension/blob/master/libs/gdx-liquidfun.jar
Download following 2 JARs and add them to the build path of your Eclipse project. If you're not using Eclipse then add them to classpath, however it will be a lot easier to do with Eclipse.
http://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx/1.5.2
http://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-jnigen/1.5.2
I have included older version from 2014, as the source code that in the git for liquidfun is using some older version of classes which are not available in new version of above JARs.
Eclipse has an option to export project as a JAR file. Use that and you're done.
If you're using plain Java then you'll have to compile all directories one by one (Java: How can I compile an entire directory structure of code ?)

How to use selected .class from .jar

I am developing an API(.jar) to be used with Android App, I have constrain on size of .jar file after build.
I am using several dependent libraries(.jar) files, but not completely; hardly 5-6 class files from 200.
How can I use the selected .class files from libraries and dependent .class files?
Proguard automatically does this.
Configure proguard via Gradle:
http://proguard.sourceforge.net/manual/gradle.html
I'm not quite sure if this is what you are looking for but you can use -cp to run a specific class in your jar file
example:
java -cp myjar.jar package1.class1

How to handle two jar files in java?

I am using Netbeans IDE for a java project. In this project i need a jar file "htmlunit-2.6.jar".
I have included this jar file in the project libraries folder. I have instantiated one of its class "WebClient" but this class needs other classes of "commons-httpclient-3.1.jar" file.
Now I have also included "commons-httpclient-3.1.jar" file in the project libraries folder. But when I compiled my source file, it throws
ClassNotFoundException: org.apache.commons.httpclient.auth.CredentialsProvider
Kindly tell me how to handle this situation when one class in one jar file needs other classes in other jar file.
Simply put the required jar files on the classpath at compile-time and it should work. If you were doing it from the command-line then it would look like this:
javac -cp jar1:jar2 my.Application
If you are using NetBeans then you need to tell NetBeans that both of the JARs are on your classpath. It will be definable in a Project > Properties wizard as described here and also here from the tutorial
The ClassNotFoundException tells you that your libraries have some dependencies that you don't have included in your classpath at runtime. Your source is OK, because if you have used something not available, NB will tell you this at compile time (or before when editing).
So, welcome in the "dependency hell" of Java. For small projects you will be able to check all dependencies by hand with readme files, docs, etc and put them in the project config as oxbow_lakes said. For bigger things look at maven. It will do (most) everything for you !
(Maven is available in NB6)

Categories