How to get my Java program to communicate with process it makes via System.in System.out
I'm trying to make a AI programming game where my Game Engine will send the game state to another Java program in the form of a string, and then wait for up to 2 seconds to receive a another string back containing the information of the move the program wants to make.
However, I'm having trouble getting constant communication between the two programs. I can send information, then receive information, but then the process just stalls out. Any advice?
The User's code must communicate through System.in and System.out, but what I do on the engine's end can be whatever.
public class Main{
public static void main(String args) throws IOException, InterruptedException{
Runtime rt = Runtime.getRuntime();
Process compileOne = rt.exec("javac PlayerOne/Main.java");
TimeUnit.SECONDS.sleep(1);
System.out.println("Compiled");
Process process = rt.exec("java PlayerOne/Main");
OutputStream stdin = process.getOutputStream(); // <- Eh?
InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
while(true){
writer.write("SENDING TO CHILD n");
writer.flush();
String line;
while((line = reader.readLine()) != null) {
System.out.println("RECEIVED: " + line);
}
}
}
}
public class Main{
public static void main(String args) throws Exception{
System.out.println("SENDING TO OG PROCESS");
Random r = new Random();
Scanner console = new Scanner(System.in);
while(true){
String received = console.nextLine();
System.out.println("CHILD RECEIVED: " + received);
System.out.println(r.nextInt());
}
}
}
But here's my output:
Compiled
RECEIVED: SENDING TO OG PROCESS
RECEIVED: CHILD RECEIVED: SENDING TO CHILD
RECEIVED: -800346246
java input process io output
add a comment |
I'm trying to make a AI programming game where my Game Engine will send the game state to another Java program in the form of a string, and then wait for up to 2 seconds to receive a another string back containing the information of the move the program wants to make.
However, I'm having trouble getting constant communication between the two programs. I can send information, then receive information, but then the process just stalls out. Any advice?
The User's code must communicate through System.in and System.out, but what I do on the engine's end can be whatever.
public class Main{
public static void main(String args) throws IOException, InterruptedException{
Runtime rt = Runtime.getRuntime();
Process compileOne = rt.exec("javac PlayerOne/Main.java");
TimeUnit.SECONDS.sleep(1);
System.out.println("Compiled");
Process process = rt.exec("java PlayerOne/Main");
OutputStream stdin = process.getOutputStream(); // <- Eh?
InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
while(true){
writer.write("SENDING TO CHILD n");
writer.flush();
String line;
while((line = reader.readLine()) != null) {
System.out.println("RECEIVED: " + line);
}
}
}
}
public class Main{
public static void main(String args) throws Exception{
System.out.println("SENDING TO OG PROCESS");
Random r = new Random();
Scanner console = new Scanner(System.in);
while(true){
String received = console.nextLine();
System.out.println("CHILD RECEIVED: " + received);
System.out.println(r.nextInt());
}
}
}
But here's my output:
Compiled
RECEIVED: SENDING TO OG PROCESS
RECEIVED: CHILD RECEIVED: SENDING TO CHILD
RECEIVED: -800346246
java input process io output
Have you considered usingSocket
s instead?
– MadProgrammer
Nov 13 '18 at 23:30
BufferedReader
is waiting for a "end of line" terminator, butnextInt
will never send one
– MadProgrammer
Nov 13 '18 at 23:34
@MadProgrammer Wouldn't the "ln" of "println" send the "end of line" terminator? (the n)
– WillN210
Nov 13 '18 at 23:41
That’s a reasonable assumption, but it still seems to be a point of failure
– MadProgrammer
Nov 14 '18 at 0:14
add a comment |
I'm trying to make a AI programming game where my Game Engine will send the game state to another Java program in the form of a string, and then wait for up to 2 seconds to receive a another string back containing the information of the move the program wants to make.
However, I'm having trouble getting constant communication between the two programs. I can send information, then receive information, but then the process just stalls out. Any advice?
The User's code must communicate through System.in and System.out, but what I do on the engine's end can be whatever.
public class Main{
public static void main(String args) throws IOException, InterruptedException{
Runtime rt = Runtime.getRuntime();
Process compileOne = rt.exec("javac PlayerOne/Main.java");
TimeUnit.SECONDS.sleep(1);
System.out.println("Compiled");
Process process = rt.exec("java PlayerOne/Main");
OutputStream stdin = process.getOutputStream(); // <- Eh?
InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
while(true){
writer.write("SENDING TO CHILD n");
writer.flush();
String line;
while((line = reader.readLine()) != null) {
System.out.println("RECEIVED: " + line);
}
}
}
}
public class Main{
public static void main(String args) throws Exception{
System.out.println("SENDING TO OG PROCESS");
Random r = new Random();
Scanner console = new Scanner(System.in);
while(true){
String received = console.nextLine();
System.out.println("CHILD RECEIVED: " + received);
System.out.println(r.nextInt());
}
}
}
But here's my output:
Compiled
RECEIVED: SENDING TO OG PROCESS
RECEIVED: CHILD RECEIVED: SENDING TO CHILD
RECEIVED: -800346246
java input process io output
I'm trying to make a AI programming game where my Game Engine will send the game state to another Java program in the form of a string, and then wait for up to 2 seconds to receive a another string back containing the information of the move the program wants to make.
However, I'm having trouble getting constant communication between the two programs. I can send information, then receive information, but then the process just stalls out. Any advice?
The User's code must communicate through System.in and System.out, but what I do on the engine's end can be whatever.
public class Main{
public static void main(String args) throws IOException, InterruptedException{
Runtime rt = Runtime.getRuntime();
Process compileOne = rt.exec("javac PlayerOne/Main.java");
TimeUnit.SECONDS.sleep(1);
System.out.println("Compiled");
Process process = rt.exec("java PlayerOne/Main");
OutputStream stdin = process.getOutputStream(); // <- Eh?
InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
while(true){
writer.write("SENDING TO CHILD n");
writer.flush();
String line;
while((line = reader.readLine()) != null) {
System.out.println("RECEIVED: " + line);
}
}
}
}
public class Main{
public static void main(String args) throws Exception{
System.out.println("SENDING TO OG PROCESS");
Random r = new Random();
Scanner console = new Scanner(System.in);
while(true){
String received = console.nextLine();
System.out.println("CHILD RECEIVED: " + received);
System.out.println(r.nextInt());
}
}
}
But here's my output:
Compiled
RECEIVED: SENDING TO OG PROCESS
RECEIVED: CHILD RECEIVED: SENDING TO CHILD
RECEIVED: -800346246
java input process io output
java input process io output
edited Nov 13 '18 at 23:38
WillN210
asked Nov 13 '18 at 23:24
WillN210WillN210
134
134
Have you considered usingSocket
s instead?
– MadProgrammer
Nov 13 '18 at 23:30
BufferedReader
is waiting for a "end of line" terminator, butnextInt
will never send one
– MadProgrammer
Nov 13 '18 at 23:34
@MadProgrammer Wouldn't the "ln" of "println" send the "end of line" terminator? (the n)
– WillN210
Nov 13 '18 at 23:41
That’s a reasonable assumption, but it still seems to be a point of failure
– MadProgrammer
Nov 14 '18 at 0:14
add a comment |
Have you considered usingSocket
s instead?
– MadProgrammer
Nov 13 '18 at 23:30
BufferedReader
is waiting for a "end of line" terminator, butnextInt
will never send one
– MadProgrammer
Nov 13 '18 at 23:34
@MadProgrammer Wouldn't the "ln" of "println" send the "end of line" terminator? (the n)
– WillN210
Nov 13 '18 at 23:41
That’s a reasonable assumption, but it still seems to be a point of failure
– MadProgrammer
Nov 14 '18 at 0:14
Have you considered using
Socket
s instead?– MadProgrammer
Nov 13 '18 at 23:30
Have you considered using
Socket
s instead?– MadProgrammer
Nov 13 '18 at 23:30
BufferedReader
is waiting for a "end of line" terminator, but nextInt
will never send one– MadProgrammer
Nov 13 '18 at 23:34
BufferedReader
is waiting for a "end of line" terminator, but nextInt
will never send one– MadProgrammer
Nov 13 '18 at 23:34
@MadProgrammer Wouldn't the "ln" of "println" send the "end of line" terminator? (the n)
– WillN210
Nov 13 '18 at 23:41
@MadProgrammer Wouldn't the "ln" of "println" send the "end of line" terminator? (the n)
– WillN210
Nov 13 '18 at 23:41
That’s a reasonable assumption, but it still seems to be a point of failure
– MadProgrammer
Nov 14 '18 at 0:14
That’s a reasonable assumption, but it still seems to be a point of failure
– MadProgrammer
Nov 14 '18 at 0:14
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290972%2fhow-to-get-my-java-program-to-communicate-with-process-it-makes-via-system-in-sy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290972%2fhow-to-get-my-java-program-to-communicate-with-process-it-makes-via-system-in-sy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you considered using
Socket
s instead?– MadProgrammer
Nov 13 '18 at 23:30
BufferedReader
is waiting for a "end of line" terminator, butnextInt
will never send one– MadProgrammer
Nov 13 '18 at 23:34
@MadProgrammer Wouldn't the "ln" of "println" send the "end of line" terminator? (the n)
– WillN210
Nov 13 '18 at 23:41
That’s a reasonable assumption, but it still seems to be a point of failure
– MadProgrammer
Nov 14 '18 at 0:14