Java
    • PDF

    Java

    • PDF

    Article Summary

    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.

    Note

    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:

    1. 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;
          }
      }
      
    2. Use commands to compile Hello.java in hello.jar, which is a JAR file.

      $ javac Hello.java
      
      $ jar cvf hello.jar Hello.class
      
    3. Upload the hello.jar file created, and create an action called helloJava.
      compute-15-2-301.png

      • Main class: Set to the Hello class.

    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.
    compute-15-2-302.png

    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.
    compute-15-2-303.png

    Caution

    When you compile a Java file, google-gson must be included in the Java CLASSPATH.


    Was this article helpful?

    What's Next
    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.