--- title: "Java" slug: "cloudfunctions-example-java" updated: 2026-09-17T09:31:52Z published: 2026-09-17T09:31:52Z canonical: "guide.ncloud-docs.com/cloudfunctions-example-java" --- > ## Documentation Index > Fetch the complete documentation index at: https://guide.ncloud-docs.com/llms.txt > Use this file to discover all available pages before exploring further. # Java Classic/VPC環境で利用できます。 Java形式のアクションを作成して多様に活用する方法と、そのユースケースを紹介します。 ## アクション作成 Javaアクションを作成する手順は JavaScriptと Swift形式のアクションを作成する手順と似ています。 参考 作成されたアクションをコンパイルしてテストするには、必ず [JDK 8](http://www.oracle.com/technetwork/java/javase/downloads/index.html)がローカル環境に設定されている必要があります。 Javaアクションは mainメソッドを持つ Javaプログラムです。そのため、次のような形式でメソッドが必ず定義されている必要があります。 ``` public static com.google.gson.JsonObject main(com.google.gson.JsonObject); ``` この点に注意して、Java形式のアクションを作成するユースケースは、次の通りです。 1. 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. コマンドを使用して Hello.javaを JARファイルである hello.jarにコンパイルします。 ``` $ javac Hello.java ``` ``` $ jar cvf hello.jar Hello.class ``` 3. 作成された hello.jarファイルをアップロードして helloJavaという名前のアクションを作成します。 ![cloudfunctions-example-java_v2_01_ko](https://cdn.document360.io/6998976f-9d95-4df8-b847-d375892b92c2/Images/Documentation/cloudfunctions-example-java_v2_01_ja.png?sv=2026-02-06&spr=https&st=2026-10-26T17%3A47%3A35Z&se=2026-10-26T18%3A47%3A35Z&sr=c&sp=r&sig=sBOsikobi3YsQ17fDYtfRQOWMXaoO0OV0zuZewCiCEY%3D) > メインクラス: Helloクラスに設定 もし、クラスがデフォルトパッケージでない場合、com.example.MyMainのようにパッケージを含むクラス名である Java fully-qualified class name(FQCN)を使用できます。 ![compute-15-2-302_ko](https://cdn.document360.io/6998976f-9d95-4df8-b847-d375892b92c2/Images/Documentation/compute-15-2-302_ja.png?sv=2026-02-06&spr=https&st=2026-10-26T17%3A47%3A35Z&se=2026-10-26T18%3A47%3A35Z&sr=c&sp=r&sig=sBOsikobi3YsQ17fDYtfRQOWMXaoO0OV0zuZewCiCEY%3D) Javaアクションのメソッド名まで変更して使用したい場合、methodNameのようにメソッド名を指定してアクションを作成することもできます。 ![compute-15-2-303_ko](https://cdn.document360.io/6998976f-9d95-4df8-b847-d375892b92c2/Images/Documentation/compute-15-2-303_ja.png?sv=2026-02-06&spr=https&st=2026-10-26T17%3A47%3A35Z&se=2026-10-26T18%3A47%3A35Z&sr=c&sp=r&sig=sBOsikobi3YsQ17fDYtfRQOWMXaoO0OV0zuZewCiCEY%3D) 注意 Javaファイルのコンパイル時、[google-gson](https://github.com/google/gson)は必ず Java CLASSPATHに含む必要があります。