Last active
September 26, 2016 14:23
-
-
Save asgs/eb2cdeaf04dfbf00f3f7ec1d5809d27e to your computer and use it in GitHub Desktop.
JShell Connector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.asgs; | |
/*import org.zeroturnaround.exec.ProcessExecutor; | |
import org.zeroturnaround.exec.StartedProcess; | |
import org.zeroturnaround.exec.stream.PumpStreamHandler;*/ | |
import java.io.*; | |
import java.nio.charset.StandardCharsets; | |
import java.util.Scanner; | |
import java.util.concurrent.ExecutionException; | |
/** | |
* Main program! | |
*/ | |
public class Main { | |
private static final String LINE_SEPARATOR = System.getProperty("line.separator"); | |
public static void main(String[] args) throws IOException, InterruptedException, ExecutionException { | |
doItManually(); | |
//doItUsingZtExecLib(); | |
} | |
/*private static void doItUsingZtExecLib() throws IOException, ExecutionException, InterruptedException { | |
ProcessExecutor processExecutor = new ProcessExecutor("cmd.exe", "G:\\dev\\jdk-9\\bin\\jshell.exe"); | |
processExecutor.readOutput(true); | |
processExecutor.redirectErrorStream(true); | |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | |
processExecutor.redirectOutput(byteArrayOutputStream); | |
App app = new App(); | |
StartedProcess startedProcess = processExecutor.start(); | |
Process process = startedProcess.getProcess(); | |
InputStream inputStream = process.getInputStream(); | |
InputReaderJob inputReaderJob = app.new InputReaderJob(inputStream); | |
processExecutor.streams(new PumpStreamHandler(System.out, System.out, System.in)); | |
Thread thread1 = new Thread(inputReaderJob); | |
thread1.start(); | |
//thread1.setDaemon(true); | |
}*/ | |
private static void doItManually() throws IOException, InterruptedException { | |
ProcessBuilder command = new ProcessBuilder("/usr/local/google/home/gowrisankar/Downloads/jdk-9/bin/jshell"); | |
command.redirectErrorStream(true); | |
//command.inheritIO(); | |
System.out.println("Bootstrapping JShell. Please wait..."); | |
Process process = command.start(); | |
Runtime.getRuntime().addShutdownHook(new Thread() { | |
public void run() { | |
System.out.println("Shutdown the child process."); | |
process.destroy(); | |
} | |
}); | |
//System.out.println(process.getPid()); | |
InputStream inputStream = process.getInputStream(); | |
Main app = new Main(); | |
InputReaderJob inputReaderJob = app.new InputReaderJob(inputStream); | |
Thread thread1 = new Thread(inputReaderJob); | |
thread1.setName("JShellInputReadingThread."); | |
thread1.setDaemon(true); | |
thread1.start(); | |
OutputStream outputStream = process.getOutputStream(); | |
Scanner scanner = new Scanner(System.in); | |
Runnable scannerRunnable = () -> { | |
while (scanner.hasNextLine()) { | |
String nextLine = scanner.nextLine(); | |
nextLine += LINE_SEPARATOR; | |
try { | |
outputStream.write(nextLine.getBytes(StandardCharsets.UTF_8)); | |
outputStream.flush(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
scanner.reset(); | |
} | |
} | |
}; | |
Thread thread2 = new Thread(scannerRunnable); | |
thread2.setName("UserInputReadingThread."); | |
thread2.setDaemon(true); | |
thread2.start(); | |
process.waitFor(); | |
} | |
private class InputReaderJob implements Runnable { | |
InputStream inputStream; | |
InputReaderJob(InputStream inputStream) { | |
this.inputStream = new BufferedInputStream(inputStream); | |
} | |
@Override | |
public void run() { | |
int bytesRead = -1; | |
byte[] data = new byte[8192]; | |
try { | |
while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) { | |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | |
byteArrayOutputStream.write(data, 0, bytesRead); | |
System.out.print(new String(byteArrayOutputStream.toByteArray(), "UTF-8")); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment