forked from marenas1/Finance-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparent.java
More file actions
70 lines (62 loc) · 1.86 KB
/
parent.java
File metadata and controls
70 lines (62 loc) · 1.86 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.io.*;
class Parent
{
public static void main(String[] args)
{
// The process object that will host the Child:
Process proc = null;
// First, compile the Child:
try
{
Process processCompile = Runtime.getRuntime().exec("javac Child.java");
}
catch (IOException e)
{
e.printStackTrace();
}
// Sleep for 1 second to wait for the compiled CLASS file to be ready:
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
// Then, exec() the Child:
try
{
// Command to create an external process
String command = "java -cp dir";
// Running the above command
Runtime run = Runtime.getRuntime();
proc = run.exec(command);
}
catch (IOException e)
{
e.printStackTrace();
}
// Finally, print the Child program's output:
// Based on: https://stackoverflow.com/a/12746055/14167156
try
{
printLines("Child says:", proc.getInputStream());
printLines("Any errors:", proc.getErrorStream());
}
catch (Exception e)
{
e.printStackTrace();
}
}
/* Function for printing output: */
/* Taken from https://stackoverflow.com/a/12746055/14167156 */
private static void printLines(String name, InputStream ins) throws Exception
{
String line = null;
BufferedReader in = new BufferedReader(new InputStreamReader(ins));
while ((line = in.readLine()) != null)
{
System.out.println(name + " " + line);
}
}
}