aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/java-native/src/main/java/com/example/myproject/Greeter.java
diff options
context:
space:
mode:
Diffstat (limited to 'examples/java-native/src/main/java/com/example/myproject/Greeter.java')
-rw-r--r--examples/java-native/src/main/java/com/example/myproject/Greeter.java49
1 files changed, 49 insertions, 0 deletions
diff --git a/examples/java-native/src/main/java/com/example/myproject/Greeter.java b/examples/java-native/src/main/java/com/example/myproject/Greeter.java
new file mode 100644
index 0000000000..4123f2e202
--- /dev/null
+++ b/examples/java-native/src/main/java/com/example/myproject/Greeter.java
@@ -0,0 +1,49 @@
+package com.example.myproject;
+
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.util.Scanner;
+
+/**
+ * Prints a greeting which can be customized by building with resources and/or passing in command-
+ * line arguments.
+ *
+ * <p>Building and running this file will print "Hello world". Build and run the
+ * //examples/java-native/src/main/java/com/example/myproject:hello-world target to demonstrate
+ * this.</p>
+ *
+ * <p>If this is built with a greeting.txt resource included, it will replace "Hello" with the
+ * contents of greeting.txt. The
+ * //examples/java-native/src/main/java/com/example/myproject:hello-resources target demonstrates
+ * this.</p>
+ *
+ * <p>If arguments are passed to the binary on the command line, the first argument will replace
+ * "world" in the output. See //examples/java-native/src/test/java/com/example/myproject:hello's
+ * argument test.</p>
+ */
+public class Greeter {
+ static PrintStream out = System.out;
+
+ public static String convertStreamToString(InputStream is) throws Exception {
+ try (Scanner s = new Scanner(is)) {
+ s.useDelimiter("\n");
+ return s.hasNext() ? s.next() : "";
+ }
+ }
+
+ public void hello(String obj) throws Exception {
+ String greeting = "Hello";
+ InputStream stream = Greeter.class.getResourceAsStream(
+ "/examples/java-native/src/main/resources/greeting.txt");
+ if (stream != null) {
+ greeting = convertStreamToString(stream);
+ }
+ out.println(greeting + " " + obj);
+ }
+
+ public static void main(String... args) throws Exception {
+ Greeter g = new Greeter();
+ String obj = args.length > 0 ? args[0] : "world";
+ g.hello(obj);
+ }
+}