Using java libraries [duplicate] - java

This question already has answers here:
How to include libraries in Java without using an IDE
(6 answers)
Closed 8 years ago.
I'm trying to use a java lib to connect to a database, im doing the tutorial outlined here, http://www.homeandlearn.co.uk/java/connect_to_a_database_using_java_code.html
and I can get it to work in netbeans by putting the .jar into the library file in my project file but I don't have a clue how to get it to work hen using an ide such as vim. can someone help?

If I got your question right, you can find the right answer on this other StackOverflow post: how to include libraries in java without using an IDE
You need to build both files into a JAR file, then you can run it.
I hope this helps.

A build tool which features dependency management such as Maven will help solve this problem. To use Maven you will add the location of libraries you require to an XML file (pom.xml) and Maven will download them for you and incorporate them in your target application archive (e.g. a war file).
Maven central provides a means to search for dependencies. In your case you can find the Derby client jar here. Click on the version of the library you would like to use and you'll see the sections of build descriptor code you needs to copy and paste in for: Maven, Ivy, and a few other popular build/dependency management tools.
Once you are comfortable with Maven, you could look up the Maven "Shade" plugin which will help you create a single jar file containing the necessary dependencies to run your application.

Vim, is more of an editor than an IDE, so you will need to specify the library .jar files manually on your class path when running your application.
You can do this by specifying the -cp parameter to the Java executable. e.g.
java -cp driver.jar MyMainClass

The way you are trying to use the library you desired requires manually adjusting your CLASSPATH. Oracle has some detailed instructions on that: PATH and CLASSPATH.
Though, I would suggest to use some kind of build tool which makes it easier to handle your applications dependencies.
In the Java world, the most convenient tools are Ant, Maven and Gradle.
I feel you have just started Java development, so I would prefer Gradle in your case, although Maven is still pretty widely used in projects.
With these build tools handling dependencies will not be -lets say- wired in the IDE and you can easily build your application in any environment.

Related

How can I use JFlex if i´m cloning a project from github

i'm currently working on a Lexer in Java, i'm using jflex to do this, when my partner runs the code it works, he pushed it into a repo in github. I cloned it and when trying to run it, it says it cannot find a symbol which is Lexer, this is a class that jflex creates. I
I was expecting to be able to use the jflex library, which is stored in the lib directory in the project, also, in the .vscode directory we created a settings.json in order to be able to use it, we thought it worked but apparently when someone clones the repo it doesn't work. How can i solve this?
Download needed library
Commonly, projects uploaded to GitHub omit the various libraries needed by the app.
So you will need to manually obtain the needed library, usually a JAR file. Then place that JAR file in a location accessible by your project.
When deploying your app, you will need to either include the library within your app, or make the library available on the runtime computer.
Dependency management & build tool
Needed libraries are referred to as “dependencies”.
If doing any serious work in Java, I strongly recommend you learn how to use an IDE (such as IntelliJ, Eclipse, or NetBeans) in conjunction with a dependency management tool (such as Apache Maven or Apache Gradle).
Tools like Maven & Gradle will automatically locate a copy of your declared dependency libraries, automatically download from a repository, and then install within your project.
These tools (IDEs, and dependency managers) do come with a learning curve. But that learning time is well spent if you are invested in being a Java developer.

How to use NaCl with Java under Windows

I recently found the library Libsodium and was glad that there are also Bindings for Java. Unfortunately, the libraries don't appear to work with Windows because they were developed for other systems.
How can I use the libraries under Windows (if possible)?
Are there any alternative libraries for Java?
On that page for other bindings, you find the Java binding is called Kalium. You'll see it requires Apache Maven. So I went and got that (at the bottom it explains how to install maven, but basically you just add some environment variables).
Now on to kalium, I got it from their abstract on github. I needed mvn clean install from the directory in order to install it in Maven. After changing directory to the unzipped Kalium folder I ran the specified command (which is telling Maven to get the files and install Kalium). This will handle the rest of the set-up.
Then it's just a matter of adding it as a dependency in your Maven projects as per the Kalium readme. As for libsodium: it is a library that works with MinGW, and VisualStudio in Windows. So if you want to do C/C++ coding you can use that instead. But for the Java binding it seems you'll have deploy it using Maven.

Java import from root of jar

I've used a 17 version of Guava for my library project and extracted it into the jar. Other project uses my library but it also has a much older version of Guava. When I try to run application, it ignores new Guava and causes errors like
java.lang.NoSuchMethodError: com.google.common.cache.CacheBuilder.maximumSize(J)Lcom/google/common/cache/CacheBuilder;
How to force my project to use a new Guava and application to use an old?
I am assuming that your project and applications are distinct entities.Also, by Project I assume that you are referring to an Eclipse/IntelliJ/Netbeans project
You would need to set the correct classpaths for the Project and your application separately.
If you are using the IDE to debug your project, your project would need to include the new JAR that you have downloaded. Each IDE has it's own mechanism for adding dependencies to the Project's classpath and hence you would want to refer to the documentation related to that IDE.
For your application you can launch it as java MyApp -cp "path to the old JAR". If you are packaging your application as a JAR, make sure you are packaging the older version of Guava.
Hope this helps.
Edit based on the discussion with the owner of the question
The query here is similar to this SO query. The right way to hence resolve this issue would be either install a Custom ClassLoader or use OSGi

Add Java Libraries to Eclipse

I am quite new to Java and Eclipse.
I am trying to call some functions from the jpf library from a program that I am writing in Eclipse.
Can anyone help me load or install these libraries into my Eclipse project? The .zip files are here. If these are already in the Eclipse program or there is some better way to get them, can someone also post that answer in addition to how to load any arbitrary java library.
Thanks for the help.
The JPF project page, to which you already linked in your question, contains a section about setting up the Eclipse IDE for JPF projects. A more general webpage on how to add external jars to your eclipse project might be helpful as well
download desired jar in to your local machine - add those jar to your project using below
right click on your project - configure build path - libraries - add
external jars - browse downloaded jars - add
Download and extract jpf-bin-1.5.1.zip. In here, you will find one or more JAR files. Add them into your Eclipse project, under something like a "lib" folder. Right-click on the JAR files from there, then choose "Build Path" / "Add to Build Path".
Or better yet, investigate the use of Apache Maven, and let it handle all of the dependency resolution for you. (Per http://jpf.sourceforge.net/roadmap.html, this project is at least Maven-enabled.) At least the 1.5 version is also available from the central Maven repository at http://repo1.maven.org/maven2/net/sf/jpf/jpf/. I don't see 1.5.1, but the jpf-bin-1.5.1.zip contains a "MAVEN.txt" that explains how to install it into a local Maven repository for use.
check how add libraries in eclipse.
http://wiki.eclipse.org/FAQ_How_do_I_add_an_extra_library_to_my_project%27s_classpath%3F
Also check this how make user libraries in Eclipse and then add them in your project.
http://i-proving.com/2006/07/18/user-libraries-in-eclipse

can eclipse plugin project depend on java project

I have an eclipse plugin project which dependes on java project in my eclipse. usually what I did is export the project as jar and use it as-is in the plugin. but this requires manual work. can I have a reference from my plugin projct to a java project that will be both compile-time and run-time dependency ?
I saw a similar question, but not exactly the same.
I think, the closest thing to this is to create a jar file from the referenced project, and import it to the projects repository. But thats quite hard to manage for a currently developed project.
On the other hand, isn't it possible to simply convert the Java project into a plug-in permanently? If the other user does not use OSGi/Eclipse, he/she will see only a manifest/manifest.mf file (and possibly a plugin.xml) next to the java project specific stuff, so this would not disturb them, but would help you.

Categories

Resources