How to use 3rd party packages in Java - java

I'm developping my first Java application which actually needs a 3rd party package and now I'm lost as to how to actually use it. The packages I need are from VLCJ so that I can embed a media player in my GUI.
Usually, I can just import packages and classes, but is this possible with 3rd party packages? They have a .jar file to download at their website, are the packages stored in that? And if so, how do I go about using them in my own application?

You just need the third party JAR to be on your project's classpath. What IDE are you using? In Eclipse you would do:
Go to Package Explorer window on the
left. Select the Java project you are
working on and right click. Click
Properties. Then click Java Build
Path. Click Add External Jars.
Or you could modify your system wide CLASSPATH to include the JAR. Or you can do it on the command line e.g.
java -classpath C:\java\thirdpartjars\thirdparty.jar MyProgram
(you can use the argument with javac too).
There are many ways to crack this nut.

Yes, the JAR file you download is an archive (basically a .zip file) of compiled .class files which you can then import into your own application.
The only thing is you need to add the .jar file to your application's classpath for you to use it before you can import it.
I would suggest looking at a good Java book or tutorial (for example, the official Java tutorial) as this is all stuff that should be covered.

You need to add the jar file to the search path of javac when you compile your project; and you need to make the jar available at runtime-- it needs to be in the classpath of the java process that runs your program.
If you are using an IDE, you usually update these paths in the project settings.

Related

Javac does compiling always have to be from top of package? (Passing path of package to javac ?)

When compiling java code the I have been told the compiler must be run from the top of the package.
That is if I am trying to compile Test.java which is in tools.testing I have to first set the top of the package hierarchy, the folder containing /tools in order for it to work.
The class I am trying to compile uses another class contained in the same package and as such passing the full path of the code to the compiler prevents it from seeing the other class (as it doesn't search current directory and instead searches for the package inside of itself: ./tools/testing when it is already in /tools/testing )
I wanted to know if this was always the case or if there was a way to, for example: provide the path to the top of the package (since passing full path will not work for me) as an argument of the javac command or something similar ?
Thanks !
You should use an Integrated Development Environment (IDE) like IntelliJ, Eclipse or Netbeans. In an IDE you can create a Java project which has a directory acting as the 'source root'.
If you use Maven as your build tool the default location for such a directory is /src/main/java/ (this is the de-facto standard for Java projects at this time).
The IDE will automatically compile your Java files for you and allow you to run them easily during development.
If you want to run the application stand-alone you have to package it in some way. One simple and effective way is to generate a .jar file which contains all the .class files and other files you need (like images, .properties files etc). If you specify a pom.xml file for your project (that's Maven again) and set the packaging to jar Maven will automatically create a .jar file for you. You can even make the .jar file runnable with some additional settings.
See also this answer for some more info about packaging.

Create Java projects without eclipse?

Is there any way I can create Java projects using a simple text editor? Not an IDE like eclipse?
I want to be able to create .jar files without the assistance of an IDE, I have all the JDK commands installed already on my computer (such as javac)
I'd like to know what file structure I need, how I need to arrange my class files, what compilation steps I need to go through etc. to be able to create jar files.
Yes, completely doable (just not much fun once the project gets bigger).
I suggest if it's not a throwaway project, use a build tool like Maven or Gradle to manage your build process, so that you don't need to assemble classpaths and resources yourself, but still retain full control of the build and test lifecycle, without IDEs. This comes at a complexity cost, of course, but once it's set up life becomes easier.
See also How can I create an executable JAR with dependencies using Maven? or the Gradle docs about creating JARs
I'd highly recommend the standard Maven source directory layout too (src/main, src/test etc) as it's both commonplace and makes for easy integration with the above tools.
Follow the below steps to create a jar file
Compile the java source using javac
Create a manifest file (if main exists) to identify main class
Use the below command to create a jar file
jar -cvfm *.class
Yeah. You can create your project structure without an IDE. But it's time consuming and you have to do everything.
To talk about creating JAR, you don't want any extra software. You can use jar utility, which comes with JDK.
Here are steps to create jar:
Compile classes which you want to in jar
Create manifest file (.mf). It's needed if you want to make jar as executable. If you want to bundle classes only together, then no need. (eg. Dependency jar)
Go to command prompt and execute following command "jar cvf MyJarName.jar *.class". Make sure java is set in environment path and you're inside the directory of classes.
cvf means "create a jar; show verbose output; specify the output jar file name.
That's all. If you want to include any folders inside jar then you can use folder name in above command after classes and it must be separated by space.
Example: jar cvf TicTacToe.jar TicTacToe.class audio images

Creating a .exe for a java project

I've developped a game during a Game Jam and I'd like to create an executable to distribute it to the other team members.
The game uses the slick2d and lwjgl library. I've tried to use JExePack, but the .exe file I get isn't runnable, I get an error while launching it.
Even the jar file gets me errors.
I'm only able to launch the game on the IDE. When I launch it with the command line : java -jar "game.jar", it obviously tells me that there's missing libraries, even if I indicate the path to the lib folder.
Is there an easy way to create an executable ?
Thanks in advance.
I think using a jar was a good idea.
You need to add every required jar in the classpath one-by-one for the jar to run properly.
Launch4J (http://launch4j.sourceforge.net/) is my favorite tool for that. You can just export an executable jar from your IDE and create an exe out of it. Creating an executable jar in Eclipse gives you the option to include all required libraries in it, which saves you from adding them manually when create the exe file. The minimal settings you need are:
Input (your executable jar)
Output (the .exe you want to create)
Minimum Java version (i.e. 1.6.0)
Thats it (as far as I remember)
If you want the exe you can use exe4j, it's a very useful tool, but i think using jars is better since you can run them on every platform. Anyway, when you export the jar, check on your ide's preferences if it automatically imports the libs. (for example, on eclipse you can pack the required libs into the exported jar)
You can use Luncher4j to create an exe file and convert the jar libraries to dll files.

How to customize java options of a independent netbeans application?

i'm developing an application using Netbeans RCP. I have added an option to add a jar to my class path in the project.properties file of my platform:
run.args.extra=-cp:a ./appclient/glassfish/lib/gf-client.jar
The problem i encounter is that is does work when i run it from the Netbeans IDE but not when i try to create a independent application (build for Mac OSX for instance). I hear that the project.properties is no longer taken in account when you run an independent application and of course my appclient directory containing the jars does not exist anymore in the application package (so my jar is not added to class path).
How can i make this -cp option works for my independent Mac OSX application?
EDIT: i was able to create a custom conf file for my independent platform but i can't find a way to add my jar to the class path, i don't know what options to use.
EDIT: i found that i need to you endorsed mechanism to achieve it. So i have added the following command to my app.conf file:
J-Djava.endorsed.dirs=/Users/altanis/appclient/glassfish/lib/gf-client.jar
But when i run the .app (mac application), i get this error:
-J-Djava.endorsed.dirs=/Users/altanis/appclient/glassfish/lib/gf-client.jar: No such file or directory
The path is correct. Do i need to make something special to make the JVM aware of this? I followed this tutorial and somewhere in the comments the author says:
Right, but the package-appclient copies everything for you and you
should be able to put it on the classpath using the endorsed
mechanism. Unpack the jar created by that and add everything you need
from there (the jars) to your application installer. Then you can use
the endorsed (-J-Djava.endorsed.dirs=${GFCLIENT_PATH}) mechanism in
your app.conf to put it on the application classpath. This way you
should be able to deploy it together with your client.
I think, that create a new library is the better way.
Create module type library with required jars
In your module add dependency to created module (type wrapped library)
You must add entry Class-path to you application's MANIFEST.MF
For example
Class-Path: apache-commons-2.1.jar ejb-api-3.0.jar
all this jars should be in the root directory of your application
Your appliction should have next structure
MyApplication.jar
/META-INF
/META-INF/MANIFEST.MF
/apache-commons-2.1.jar
/ejb-api-3.0.jar
/com/package/classes
or you can use jar tool of JDK to create a jar
read more here Oracle doc

How to run jar with JOGL app without -D key?

I tried top learn Java because it's the easiest way to make cross platform apps(python too but I've already know it). I tried to write hello world program with JOGL. I've written it and it runs perfectly in my Eclipse and NetBeans but when I'm trying to build jar and run it it says that there isn't JOGl in the java.library.path. I tried to pass library path with -D argument and it works!
The question: Is there is a way to run it without any additional args? With only double click.
Thanks.
You should create a manifest file in your project: META-INF/MANIFEST.MF (probably you already have it in your project tree).
And There you can specify your classpath, for example:
Class-Path: lib/jogl.jar lib/other_lib.jar
Then make sure that this file goes in your jar. For more information http://download.oracle.com/javase/tutorial/deployment/jar/manifestindex.html
When it comes time to deploy to users, deploy the app. using Java Web Start. That makes using natives a 'one click' install for the end-user, and it can also partition the native download between platforms.
Is there is a way to run it without any additional args? With only double click.
JWS also offers desktop integration - desktop shortcuts and menu items with icons, on the supported OS'.
You can do the above very easily using a tool like JarSplice. Just follow the gui, its pretty easy to do. The application will put the natives inside the jar for you, so all you have to do is double click the jar to run your jogl application.
Setting the Java library path is no more required in JOGL 2.0. You just need to set the class path (jogl-all.jar and gluegen-rt.jar must be in the class path) and to put the JARs containing the native libraries into the same directories whatever you use (applications, applets, Java Web Start, etc...). Then, JOGL 2.0 automatically loads the native libraries.

Categories

Resources