Why can't I import apache.thrift to an IntelliJ project? - java

New to big Java projects so I'm not too sure how dependencies work in code. If I wanted to use apache thrift, why can I not just put
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
at the top of my files? Does this require me to download something and add it to my classpath? Wouldn't that imply that anyone else trying to run my code would also need to install it locally? I'm hoping to have standalone Java files without using something like Maven or npm. Thanks in advance.

I would recommend you to use a dependency management tool like
Maven or Gradle.
Those tools will help you with your dependencies and not only (Builds, lifecycles, etc).
So what you will have to do is the following:
For Maven at your pom file, get the dependency from the Maven Repo:
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.15.0</version>
</dependency>
For Gradle at your .gradle file, again from Maven Repo:
implementation group: 'org.apache.thrift', name: 'libthrift', version: '0.15.0'
If you won't use the above tools, then you must find and download the .jar file
of the library, you want to use and add it to your project's classpath.
Go to the Apache Thrift website and there you can get the latest release.
But, to answer your question, YES they should also download the libraries you used unless you pack them all together (as Maven does it for you).
But the issue is if the other users download a different version than the one you used. Then maybe your app won't run as expected or even throw exceptions.

Related

Gradle Plugin dependency

What is the exact dependency I need to develop a Gradle Plugin in Java? Ideally I would like to get it from a well-known repository such as Maven Central or similar.
I have a Maven project with a core functionality and I just added two extra plugins, one for Ant, one for Maven. They are already tested and working; easy! Now, I wanted to add a third module for a Gradle plugin to make this functionality also available from any Gradle project.
However, I can't find the exact dependencies I need to develop a Gradle plugin.
The Gradle docs (such as https://docs.gradle.org/current/userguide/java_gradle_plugin.html) are not very well written to say the least. They mention:
the gradleAPI() dependency
or the java-gradle-plugin dependency
But they are quite unclear... no group, no version (really?).
If anyone can enlighten me to where I can get these dependencies from, I would be very thankful.
Gradle's public and internal APIs, aka gradleApi(), are bundled with the Gradle distribution and not independently published and therefore not easily consumable by Maven builds. There's the pending epic #1156 (Ensure plugin cross-version compatibility by allowing a user to depend on gradlePublicApi()) that might help here.
Since Gradle plugins are best to be built with Gradle, a pragmatic solution is to invoke the Gradle build from Maven and attach the produced artifact to the Maven build. Andres Almiray (aalmiray) once described this in the blog post Running Gradle Inside Maven (Web Archive Link). He describes the following high level steps:
Create a new Maven module (e.g. gradle-plugin) and add attach it to the parent POM
In the POM of gradle-plugin add a dependency to your core module. Use the maven-dependency-plugin to store dependencies to the Maven build folder, e.g. target/dependencies.
Create the build.gradle, add a Maven repository that points to target/dependencies (step 2) and let it depend on the core module as well as gradleApi(). Implement the Gradle plugin.
Use the exec-maven-plugin to invoke the Gradle build.
Use the maven-resources-plugin to copy the Gradle built plugin jars to the standard Maven build folder.
Use the build-helper-maven-plugin to attach the copied jars to the Maven build.
Sample project to be found here (gradle-in-maven).
https://docs.gradle.org/current/userguide/custom_plugins.html#sec:custom_plugins_standalone_project
In here it is mentioned that it is gradleApi() and I know that this works (from experience). The localGroovy() on that page is only needed if your plugin code uses groovy (does not apply if you only use groovy in the build.gradle of your plugin).
java-gradle-plugin is a library that makes it a bit simpler to make plugins, it is not required though. I personally prefer using gradleApi only.
EDIT:
It appears I've misunderstood the question. Here are the steps to get gradleApi jar:
Create a Gradle project with your desired Gradle version.
Add implementation gradleApi() dependency.
Import/run the project once.
Go to your .gradle folder (located in home folder in Linux-based operating systems).
Open caches folder
Open the version folder you want, e.g. 6.0.1
Open generated-gradle-jars folder.
Copy the jar to wherever you want and use it.
For me the 6.0.1 jar is at ~/.gradle/caches/6.0.1/generated-gradle-jars/gradle-api-6.0.1.jar
Please note that I have not tested this, I know the jar is there but I haven't tried using it.

figure out how I got a Java package

I am very new to Java. I am running somebody else's program on my computer, and they have imports like:
import weka.classifiers.CostMatrix;
import weka.classifiers.Evaluation;
import weka.classifiers.meta.CostSensitiveClassifier;
import weka.core.*;
The program actually works for me, but I am surprised because weka is a pretty specialized program, so I doubt it is distributed with Java. I never installed weka using any package manager, and I have searched the program code and it doesn't contain any weka packages explicitly.
Do you have any tips for figuring out 1) where these packages are installed, and 2) how I "got" these packages on my local computer? I have read that Java doesn't have a centralized package manager like Python or Perl do, so that might make it harder. I am super new to Java so any basic tips about package management would also be appreciated.
These packages are dependencies of your project, so they have probably been downloaded automatically by a tool that manages dependencies.
There are several possible build tools that can do that. Since you are working with Java/JVM, the usual suspects are Maven and Ant or maybe (less likely) Gradle or SBT.
In your case, the most probable scenario is:
A Maven plugin somewhere in your IDE manages the dependencies and downloads the jars (mvn in console less likely: you would have noticed if you used it)
A pom.xml build definition file lists all the dependencies
A weka dependency is probably declared somewhere in the pom, it should look roughly like this:
-
<dependency>
<groupId>nz.ac.waikato.cms.weka</groupId>
<artifactId>weka-stable</artifactId>
<version>3.8.0</version>
</dependency>
The JARs are stored in a hidden directory .m2 (or maybe .ivy) in your home directory.
The idea is that you can simply get the source code files and the pom.xml, and let Maven (or a similar build tool) download all dependencies, get all the required compiler plugins (or test-coverage tools, or whatever), and build your project. If you tried to do without a build tool, you would have to pass around eternally long lists of dependencies with version numbers that have to be obtained somehow before your program can be compiled, and this would be just a huge mess.
Edit: It is probably downloaded from here: Maven Central: weka-stable
It wouldn't run unless those packages are on the classpath and passed at runtime via
java -classpath
Or you're running an uber JAR file that does contain the libraries.
Common solutions for dependency management include a pom.xml (Maven), build.gradle (Gradle), or build.sbt (SBT).
While those aren't the only options, another solution would be those JAR libraries have been copied into your Java installation somehow

How to import Google Cloud Client Library in Java

How would one import the Google Cloud Client Library found at :
http://googlecloudplatform.github.io/google-cloud-java/0.8.0/index.html
Into eclipse, non maven, java?
I have found no .jar file in the library.
EDIT:
I tried adding the .jar file but I still get "the import could not be resolved" on the following imports:
import com.google.gcloud.AuthCredentials;
import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.DatastoreOptions;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;
Just download it from Maven Central, even if you do not want to use some proper build tool like Gradle or even Maven: https://repo1.maven.org/maven2/com/google/cloud/google-cloud/0.8.0/google-cloud-0.8.0.jar
As you do not want to use a proper build tool with transitive dependency management (like Gradle, Ant+Ivy, Maven in best-to-worst order) you of course also have to add all transitive dependencies manually. Look into the POM file of the library at https://repo1.maven.org/maven2/com/google/cloud/google-cloud/0.8.0/google-cloud-0.8.0.pom and also download and add all dependencies and their dependencies and their dependencies.
The google-cloud-0.8.0.jar is just an empty placeholder, so you cannot use something by just importing that JAR of course.
com.google.gcloud.AuthCredentials for example is actually contained in gcloud-java-core and com.google.gcloud.datastore.Datastore in gcloud-java-datastore. If you are interested in which JAR contains an actual class file you can easily use the Maven Central search for this like http://search.maven.org/#search%7Cga%7C1%7Cfc%3Acom.google.gcloud.datastore.Datastore
Get JAR file from maven central, for example for 0.8 version here is download link. And then add JAR to your Eclipse project as usual.

How to import org.apache Java dependencies w/ or w/o Maven

So the quick background is I am creating a java program, that uses many different imports
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.util.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
I know that Netbeans isn't finding these files because I do not have them on my computer. But is there a way to have Netbeans automatically connect with org.apache and retrieve these files? Or do I just have to go and download them. Someone recommended using Maven, but I am not sure if this is the right solution or how to go about that?
Thanks
Unless you use a Maven structure (see here getting started with Maven) you will have to download all jars manually.
If using only Hadoop (as in your example) this might not seem that much of a deal, but when working with big projects it is easier to declare your dependencies in a pom.xml file. It is much more easier than downloading X different jars, and you can easily move to a newer version of a library, rather than having to delete and and download another.
I saw that someone asked in a comment why people like Maven so much. Well, to be honest, I personally find it easy to use and very useful. Furthermore, a Maven project can be easily imported in IntelliJ, Eclipse or Netbeans, whereas creating for example an IntelliJ project can cause difficulties in importing it in Eclipse or NetBeans.
To get started using Maven with Netbeans, you can go to: New Project, Categories:Maven Projects:{Best Option}. Then in the project files, open pom.xml. Here is where dependencies for your project are added. If you are not sure what to insert try searching for your jar name + "maven" on the internet. The plugin for Netbeans is able to connect to the maven repository and autocomplete most fields.
Sample from: http://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.6
<project...>
....
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
...
</project>
Download the .jar file here: http://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core/0.20.2
In Eclipse, right-click on your project, click Properties, search in the text box for Library, click on Build Paths, click Add External JAR, and select the file you downloaded from the link above.
You will have to download the jar-files yourself. Unless you start using Maven or a similar dependency management tool.
You must download them. The name org.apache.hadoop is a package name, and we only use the name of the site as a convention. See this tutorial on packages for more information. Essentially a package is a folder on your computer, often in the Java\jre\lib\ext\ directory.
Refer tutorial
http://www.tutorialspoint.com/hadoop/hadoop_mapreduce.htm
It mentions :-
Download Hadoop-core-1.2.1.jar, which is used to compile and execute the MapReduce program. Visit the following link http://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core/1.2.1 to download the jar.
Not a netbeans user , but I'm sure that even in netbeans, you have a maven plugin.
"Mavenize" your project, and when you will perform mvn clean install, you will get these jars to local maven repository.
With Eclipse I use the m2Eclipse plugin and it works really well for me.
This of course depends that these jars can be found in maven repositories over the net, such as maven central repository.
I have final figured out my preferred way to create a new Hadoop project and import the dependencies using Maven.
Using NetBeans I create a new Maven project.
Then under project files, I open the pom.xml.
I finally add inside of
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>0.20.2</version>
</dependency>
After building with dependencies I am now ready to code.

LibreOffice Maven Dependencies?

I am trying to run LibreOffice's examples in a small Maven project. Netbeans does not seem to find dependencies in Maven.
For example:
import com.sun.star.awt.Point;
It seems hard to believe that LibreOffice's dependencies are not available in Maven. Or aren't they?
They are not..
http://repo1.maven.org/maven2/com/sun/star
http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.sun.star%22
Arguably that should be fixed. I would suggest to raise an issue with LibreOffice and in the mean time install your own repository server like Sonatype Nexus and deploy the needed jars there.

Categories