回答編集履歴

2

ソースに対するコメント追加

2016/03/02 06:00

投稿

MakotoMiyazaki
MakotoMiyazaki

スコア297

test CHANGED
@@ -32,7 +32,7 @@
32
32
 
33
33
 
34
34
 
35
- こんな感じのソースでそれっぽいものが行けました。
35
+ 以下のソースでそれっぽいものが行けました。
36
36
 
37
37
  ```java
38
38
 
@@ -108,8 +108,6 @@
108
108
 
109
109
  try {
110
110
 
111
- // Runtime rt = Runtime.getRuntime();
112
-
113
111
  ProcessBuilder pb = new ProcessBuilder();
114
112
 
115
113
  pb.command("java", "ExecMain");
@@ -163,3 +161,5 @@
163
161
  }
164
162
 
165
163
  ```
164
+
165
+ (各種close処理が中途半端なのはご勘弁ください。。。)

1

サンプルソース追加

2016/03/02 05:59

投稿

MakotoMiyazaki
MakotoMiyazaki

スコア297

test CHANGED
@@ -29,3 +29,137 @@
29
29
  3.処理結果を取得して検証する
30
30
 
31
31
  という感じでいけるかもしれません。
32
+
33
+
34
+
35
+ こんな感じのソースでそれっぽいものが行けました。
36
+
37
+ ```java
38
+
39
+ //標準入力を促すMainクラス
40
+
41
+ public class Main {
42
+
43
+ public static int hoge = get();
44
+
45
+
46
+
47
+ private static int get() {
48
+
49
+ try {
50
+
51
+ return System.in.read();
52
+
53
+ } catch (IOException e) {
54
+
55
+ e.printStackTrace();
56
+
57
+ }
58
+
59
+ return 0;
60
+
61
+ }
62
+
63
+ }
64
+
65
+
66
+
67
+ //そのMainクラスを実行するクラス
68
+
69
+ public class ExecMain {
70
+
71
+
72
+
73
+ public static void main(String[] args) {
74
+
75
+ System.out.println(Main.hoge);
76
+
77
+ }
78
+
79
+ }
80
+
81
+
82
+
83
+ //ExecMainを呼び出すクラス(テストクラスになるはず)
84
+
85
+ public class Sample {
86
+
87
+
88
+
89
+ public static void main(String[] args) {
90
+
91
+ String test1Result = run("first");
92
+
93
+ System.out.println("テスト1の処理結果は「" + test1Result + "」でした。");
94
+
95
+ String test2Result = run("second");
96
+
97
+ System.out.println("テスト2の処理結果は「" + test2Result + "」でした。");
98
+
99
+
100
+
101
+ }
102
+
103
+
104
+
105
+ public static String run(String input) {
106
+
107
+ String result = "";
108
+
109
+ try {
110
+
111
+ // Runtime rt = Runtime.getRuntime();
112
+
113
+ ProcessBuilder pb = new ProcessBuilder();
114
+
115
+ pb.command("java", "ExecMain");
116
+
117
+ Process p = pb.start();
118
+
119
+ PrintStream out = new PrintStream(p.getOutputStream());
120
+
121
+ out.println(input);
122
+
123
+ out.flush();
124
+
125
+ out.close();
126
+
127
+
128
+
129
+ p.waitFor();
130
+
131
+
132
+
133
+ InputStream is = p.getInputStream();
134
+
135
+ InputStreamReader isr = new InputStreamReader(is);
136
+
137
+ BufferedReader br = new BufferedReader(isr);
138
+
139
+ String tmp;
140
+
141
+ while ((tmp = br.readLine()) != null) {
142
+
143
+ result = tmp;
144
+
145
+ }
146
+
147
+ is.close();
148
+
149
+ isr.close();
150
+
151
+ br.close();
152
+
153
+ } catch (Exception e) {
154
+
155
+ e.printStackTrace();
156
+
157
+ }
158
+
159
+ return result;
160
+
161
+ }
162
+
163
+ }
164
+
165
+ ```