- Print
- PDF
Java
- Print
- PDF
Available in Classic and VPC
This document describes how to create and use Java actions in a variety of ways, and includes application examples.
Create actions
The process of creating Java actions is similar to creating JavaScript or Swift actions.
To compile and test actions created, JDK 8 must be set up in the local environment.
A Java action is a Java program with the main
method. Accordingly, the method must be defined in advance in the following format.
public static com.google.gson.JsonObject main(com.google.gson.JsonObject);
An example of creating a Java file with this taken into account is shown as follows:
Write a source code file under the name Hello.java.
import com.google.gson.JsonObject; public class Hello { public static JsonObject main(JsonObject args) { String name = "World"; String place = "Naver"; if (args.has("name")) name = args.getAsJsonPrimitive("name").getAsString(); if (args.has("place")) place = args.getAsJsonPrimitive("place").getAsString(); JsonObject response = new JsonObject(); response.addProperty("payload", "Hello, " + name + " in " + place + "!"); return response; } }
Use commands to compile Hello.java in hello.jar, which is a JAR file.
$ javac Hello.java
$ jar cvf hello.jar Hello.class
Upload the hello.jar file created, and create an action called helloJava.
- Main class: Set to the
Hello
class.
- Main class: Set to the
If the class is not the default package, then you can use the class name FQCN (Java fully-qualified class name) which includes the package, such as com.example.MyMain
.
If you'd like to change the method name of the Java action and use it, then you can create an action by specifying a method name like methodName
.
When you compile a Java file, google-gson must be included in the Java CLASSPATH.