diff -r 0535c03b2b9f src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java --- a/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java Thu Sep 08 23:43:47 2016 -0400 +++ b/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java Fri Sep 09 22:12:57 2016 +0900 @@ -36,6 +36,8 @@ import java.io.InputStreamReader; import java.util.Properties; import java.util.stream.Collectors; +import java.util.regex.Pattern; +import java.util.regex.Matcher; /* * The HotSpot implementation of com.sun.tools.attach.VirtualMachine. @@ -57,18 +59,27 @@ private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options) throws AgentLoadException, AgentInitializationException, IOException { - InputStream in = execute("load", - agentLibrary, - isAbsolute ? "true" : "false", - options); - try { - int result = readInt(in); - if (result != 0) { - throw new AgentInitializationException("Agent_OnAttach failed", result); + try (BufferedReader reader = new BufferedReader( + new InputStreamReader( + execute("load", agentLibrary, + Boolean.toString(isAbsolute), options)))) { + String result = reader.readLine(); + if (result == null) { + throw new AgentLoadException("Target VM did not respond"); + } else { + Matcher matcher = Pattern.compile("^return code: (\\d+)$") + .matcher(result); + if (matcher.matches()) { + int retCode = Integer.parseInt(matcher.group(1)); + if (retCode != 0) { + throw new AgentInitializationException( + "Agent_OnAttach failed", retCode); + } + } else { + throw new AgentLoadException(result); + } } - } finally { - in.close(); - } }