How to import Thrift into Java program? - java

I’m writing a Java-programm for school that also uses Thrift.
The problem is not so much the general programm/programm-logic itself, but just importing Thrift (to use it in a specific part).
My basic -possibly wrong- understanding is that you write the programm-code (here empfaenger.java), then you import Thrift into this file by adding the needed import-statements, e.g.:
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket ;
import org.apache.thrift.transport.TTransport;
and adding a file in the same directory from which they can actually can be imported, in this case libthrift-0.13.0.jar.(1) Then you later also import a compiled .thrift-file with the language-specific realization oft he IDL-code, that iself again imports some Thrift-classes. This file is here named syncautohersteller.
EDIT: The approach with the .jar-file was recommended by the prof.
Current project-structure (as seen in InteliJ):
The problem is now just that all the Thrift import-statements all throw errors, e.g.
empfaenger.java
java: package org.apache.thrift does not exist
syncautohersteller
package javax.annotation does not exist
so clearly i’m doing something wrong.
Does anybody know how to fix this?
(1) I got the file from the Thrift folder (Home/Downloads/thrift-0.13.0/lib/java/build/libs and then the first of the three .jar-files in the folder) after installing Thrift using ./configure, sudo make and sudo make install and trying to verify by running “~/Downloads/thrift-0.13.0$ thrift –version” with result
Thrift version 0.13.0

In IntellJ Idea to add external Jars you can find some useful information in this question: Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project.
I suggest you to manage the project's dependencies through Maven, which help you to add JAR dependencies to the classpath in a simpler way.
First, you have to convert your project into a Maven project as explained in IntelliJ Idea documentation.
Then you can follow these steps:
Go to Maven repository website
Search for Thrift
Select first result
Select the version you need
Copy the maven dependency
org.apache.thrift
libthrift
0.13.0
Add maven dependency to your pom.xml file
Execute a mvn clean install, after clicking the following button in IntelliJ
This process will help you and people which work with you to manage in a simpler way the dependencies of the project.

You can do it the simplest way with the Gradle, something like this:
build.gradle.kts:
repositories {
mavenCentral()
}
dependencies {
implementation("org.apache.thrift:libthrift:0.13.0")
}

Related

How do I write module-info requires statement from GAV coordinates of a non-modular dependency?

I am converting a legacy java app to a modular one. It is a Gradle project, and the dependencies look like this:
dependencies {
implementation 'org.jopendocument:jOpenDocument:1.3'
implementation 'xom:xom:1.3.2'
}
In the App class, my imports are:
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;
import org.jopendocument.dom.spreadsheet.MutableCell;
import org.jopendocument.dom.spreadsheet.Sheet;
import org.jopendocument.dom.spreadsheet.SpreadSheet;
I want to create a module-info.java file. But when I do, my app won't compile because the requires directives are wrong in the module-info file.
module weapongeneration4dusty.main {
requires xom;
requires jOpenDocument;
exports com.hymerfania.rptools.maptool.meta.tablegeneration;
}
The module names xom and jOpenDocument are insufficient. But I don't know what they should be for these dependencies, nor how to reconfigure Gradle; if that's what I need to do.
When I omit either requires statement, the corresponding imports fail to resolve. I have not looked in the JAR files. But I don't think either artifact is modular.
I'm using Gradle 5.6.2 and I'm targeting Java 11.

Add ical4j to a maven jhipster project

I have a jhipster generated application using maven with java springboot on the backend. I want to add the ical4j library to the project so under the <dependencies> tag in the pom.xml file I added the lines like explained here . However when I add import org.mnode.ical4j; the maven compiler throws an error package org.mnode does not exist .
This seems pretty trivial but I don't get what I am missing.
Thank you
From what I can see in the official GitHub repo (ical4j/ical4j) the correct package to import starts from net.fortuna.ical4j. Example:
import net.fortuna.ical4j.util.DefaultEncoderFactory;

How to use google/api/annotations.proto with Android

I have a problem with proto file in my project
I had import in my proto file:
import "google/api/annotations.proto";
I am getting following error when building the project.
Import "google/api/annotations.proto" was not found or had errors.
How can I use this import in my project? Should I add something to my build.gradle?
On non-Android, you could add this dependency to your build.gradle:
compile 'com.google.api.grpc:proto-google-common-protos:1.12.0'
However, Android uses Protobuf "Lite" instead of full Protobuf and there's not a pre-generated library with Lite for this proto. There is an open issue about this.
However, a workaround discussed for the well-known protos can be used here as well. Namely, use a protobuf dependency instead of a compile dependency. This will generate the code as part of your build.
protobuf 'com.google.api.grpc:proto-google-common-protos:1.12.0'
Unfortunately, this solution only really works for applications. If two libraries use this "solution" they must never be included into the same application as they will have duplicated (and potentially have different versions of) the generated classes.

Why are my Alexa Skills Kit dependencies missing?

I cloned Amazon's ASK java repository on github and ran
mvn package
on it, and it produced the following .jars:
ask-sdk-core-2.3.4.jar
ask-sdk-apache-client-2.3.4.jar
ask-sdk-dynamodb-persistence-adapter-2.3.4.jar
ask-sdk-lambda-support-2.3.4.jar
ask-sdk-servlet-support-2.3.4.jar
ask-sdk-2.3.4.jar
I noticed that when attempting to make certain imports for classes on https://developer.amazon.com/docs/custom-skills, such as import com.amazon.speech.speechlet.servlet.SpeechServlet;
I received a message stating that com.amazon.speech could not be resolved, indicating that the file didn't exist. However, after further investigation, I noticed that there was a SkillServlet class that seemed to essentially replace the SpeechServlet class as it was able to do everything that SpeechServlet could,
so I assumed that the developer site hadn't been updated yet to reflect the changes. I then noticed that RequestHandler, a class in the repository, had a method that returned an object of type Optional< Response >. When I tried to import Response with the following import:
import com.amazon.ask.model.Response;
I recieved an error message stating The type com.amazon.ask.model.Response cannot be resolved. It is indirectly referenced from required .class files
This suggests that the class definition doesn't exist in the project's classpath, despite having included all of the above .jars. I searched through and was unable to find a model directory. Did my maven build fail, and am I missing any .jars? I'm using Eclipse EE IDE, which I know is susceptible to errors, but I've cleaned my project as well as restarted the IDE to no avail.
Update:
I noticed that the pom.xml file within the ask-sdk-core-2.3.4 directory contained a dependency for ASK SDK Model, but it doesn't seem to be obtaining the dependency. I also noticed that though ask-sdk-2.3.4 should also include everything that ask-sdk-core-2.3.4 does, as the latter is listed as a dependency in the former, I have to include ask-sdk-core's jar file separately or Eclipse is not able to recognize certain classes. I think this means that maven has failed, so I just downloaded the jar directly from mvnrepository.com. Any idea why this might have happened?
If you use ask-sdk-2.3.4.jar, then it should include all the dependencies you need like you noticed in your update blurb. When you look at Hello World sample in their Github repo, the pom.xml only contains ask-sdk-2.3.4.jar but not other dependencies that ask-sdk-2.3.4.jar has in it. https://github.com/alexa/alexa-skills-kit-sdk-for-java/blob/2.0.x/samples/helloworld/pom.xml
To your error message on Response, that is because you are missing ask-sdk-model jar which you can find from maven central. http://central.maven.org/maven2/com/amazon/alexa/ask-sdk-model/
I would suggest using maven and pom.xml so that you don't have to download jars individually.

Maven Deployment package and upload to AWS-Lambda

I'm facing issue in uploading the maven deployment package to Amazon s3.
From Eclipse, I'm able to generate the .jar file successfully, however I'm facing issue in uploading to server.
Here is my Java code:
package main.java.mavantestproj;
import java.util.Map;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.lambda.runtime.Context;
public class LambdaFunctionHandler {
public String handleRequest(Map<String,Object> input, Context context) {
context.getLogger().log("Input: " + input);
AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider("mytest"));
client.setRegion(com.amazonaws.regions.Region.getRegion(Regions.US_WEST_2));
client.describeTable("ProductCatalog");
// TODO: implement your handler
return null;
}
}
in target folder i have got 2 jar's. ie lambda-java-example-1.0-SNAPSHOT.jar & original-lambda-java-example-1.0-SNAPSHOT.jar
In this first jar is 35MB & second one is 4KB. I'm not getting which one to upload to S3 to run my lambda function.
You definitely need the large "uber-jar" so your dependency classes will be included, but there is an alternative way to package things up for AWS-Lambda using the Maven assembly plugin instead of the Shade plugin. You end up with an AWS lambda deployment package in .zip format instead of a single .jar file. It will look a little more like a JEE .war file with all the original .jar dependencies kept intact, and you can include other stuff like properties files that end up unpacked in the file-system where the lambda runs (which may be a little easier to find and load in your code). If you're interested in the details, there's a blog post about it here: http://whirlysworld.blogspot.com/2016/03/aws-lambda-java-deployment-maven-build.html Also, packaging a Lambda function app this way makes it WAY easier to peek into the zip file and figure out which dependency jars are being included, and then identify the ones you might be able to exclude.
This still doesn't get Maven to handle the actual deployment of the package to AWS (create or update). Deploying, and capturing the deployment info (ARN, API-gateway app-id-url, etc.), seems to be the next thing for which Amazon hasn't provided a very clear answer or solution.
The larger JAR file that is being generated includes all of the library dependencies baked in. This is the one you will want to upload to S3 for use by AWS Lambda as these dependencies are required to run.
If you want to make this file smaller you can ensure you are only including libraries you need and remove any unnecessary ones. A common way to do this is with the AWS SDK only include the libraries for the specific services you need to call such as DynamoDB instead of including the entire AWS SDK.
It seems standalone jar file built using shade plugin is sufficient as per this AWS documentation

Categories

Resources