From 254aee40df78e79ac1b19fe6d20ae20bb05129a8 Mon Sep 17 00:00:00 2001 From: Kristina Chodorow Date: Fri, 20 Feb 2015 15:45:07 +0000 Subject: Improve Java examples -- MOS_MIGRATED_REVID=86787893 --- .../src/main/java/com/example/myproject/BUILD | 21 ++++++++++ .../main/java/com/example/myproject/Greeter.java | 45 ++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 base_workspace/examples/java-skylark/src/main/java/com/example/myproject/BUILD create mode 100644 base_workspace/examples/java-skylark/src/main/java/com/example/myproject/Greeter.java (limited to 'base_workspace/examples/java-skylark/src/main/java/com/example') diff --git a/base_workspace/examples/java-skylark/src/main/java/com/example/myproject/BUILD b/base_workspace/examples/java-skylark/src/main/java/com/example/myproject/BUILD new file mode 100644 index 0000000000..ed69d7f2b3 --- /dev/null +++ b/base_workspace/examples/java-skylark/src/main/java/com/example/myproject/BUILD @@ -0,0 +1,21 @@ +load("tools/build_rules/java_rules_skylark", "java_library", "java_binary", "java_test") + +package(default_visibility = ["//visibility:public"]) + +java_binary( + name = "hello-world", + srcs = ["Greeter.java"], + main_class = "com.example.myproject.Greeter", +) + +java_binary( + name = "hello-data", + main_class = "com.example.myproject.Greeter", + deps = [":hello-lib"], +) + +java_library( + name = "hello-lib", + srcs = ["Greeter.java"], + data = ["//examples/java-skylark/src/main/resources:greeting"], +) diff --git a/base_workspace/examples/java-skylark/src/main/java/com/example/myproject/Greeter.java b/base_workspace/examples/java-skylark/src/main/java/com/example/myproject/Greeter.java new file mode 100644 index 0000000000..b4d8516dc9 --- /dev/null +++ b/base_workspace/examples/java-skylark/src/main/java/com/example/myproject/Greeter.java @@ -0,0 +1,45 @@ +package com.example.myproject; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.PrintStream; +import java.util.Scanner; + +/** + * Prints a greeting which can be customized by building with data and/or passing in command- + * line arguments. + */ +public class Greeter { + static PrintStream out = System.out; + + public static String getRunfiles() { + String path = System.getenv("JAVA_RUNFILES"); + if (path == null) { + path = System.getenv("TEST_SRCDIR"); + } + return path; + } + + public static String convertStreamToString(InputStream is) throws Exception { + Scanner s = new Scanner(is).useDelimiter("\n"); + return s.hasNext() ? s.next() : ""; + } + + public void hello(String obj) throws Exception { + String greeting = "Hello"; + try { + String greetFile = getRunfiles() + "/examples/java-skylark/src/main/resources/greeting.txt"; + greeting = convertStreamToString(new FileInputStream(greetFile)); + } catch (FileNotFoundException e) { + // use default. + } + 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); + } +}; -- cgit v1.2.3