aboutsummaryrefslogtreecommitdiffhomepage
path: root/base_workspace/examples/java_oss/Greeter.java
blob: 39aba02c9757435aa6cbcc1b705ec62988491621 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;

class Greeter {
  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_oss/greeting.txt";
      greeting = convertStreamToString(new FileInputStream(greetFile));
    } catch (FileNotFoundException e) {
      // use default.
    }
    System.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);
  }
};