@@ -12,6 +12,8 @@ import { commandExists, canAccess } from '../util/fs';
12
12
13
13
type JvmTarget = { pid : string , name : string , interceptedByProxy : number | undefined } ;
14
14
15
+ const OLD_JAVA_MISSING_ATTACH_CLASS = 'com/sun/tools/attach/AgentLoadException' ;
16
+
15
17
// Check that Java is present, and that it's compatible with agent attachment:
16
18
const javaBinPromise : Promise < string | false > = ( async ( ) => {
17
19
// Check what Java binaries might exist:
@@ -45,15 +47,29 @@ const javaBinPromise: Promise<string | false> = (async () => {
45
47
) [ 0 ] ;
46
48
47
49
if ( javaTestResults . length && ! bestJava ) {
48
- // If some Java is present, but none are working, we report the failures. Hoping that this will hunt
49
- // down some specific incompatibilities that we can better work around/detect.
50
+ // Some Java binaries are present, but none are usable. Log the error output for debugging:
50
51
javaTestResults . forEach ( ( testResult ) => {
51
52
console . log ( `Running ${ testResult . javaBin } :` ) ;
52
53
console . log ( testResult . output . stdout ) ;
53
54
console . log ( testResult . output . stderr ) ;
54
55
} ) ;
55
56
56
- throw new Error ( `JVM attach not available, exited with ${ javaTestResults [ 0 ] . output . exitCode } ` ) ;
57
+ // The most common reason for this is that outdated Java versions (most notably Java 8) don't include
58
+ // the necessary APIs to attach to remote JVMs. That's inconvenient, but unavoidable & not unusual.
59
+ // Fortunately, I think most active Java developers do have a recent version of Java installed.
60
+ const nonOutdatedJavaErrors = javaTestResults . filter ( ( { output } ) =>
61
+ ! output . stderr . includes ( OLD_JAVA_MISSING_ATTACH_CLASS ) &&
62
+ ! output . stdout . includes ( OLD_JAVA_MISSING_ATTACH_CLASS )
63
+ ) ;
64
+
65
+ if ( nonOutdatedJavaErrors . length === 0 ) {
66
+ console . warn ( 'Only older Java versions were detected - Java attach APIs are not available' ) ;
67
+ return false ;
68
+ } else {
69
+ // If we find any other unexpected Java errors, we report them, to aid with debugging and
70
+ // detecting issues with unusual JVMs.
71
+ throw new Error ( `JVM attach test failed unusually - exited with ${ nonOutdatedJavaErrors [ 0 ] . output . exitCode } ` ) ;
72
+ }
57
73
} else if ( bestJava ) {
58
74
return bestJava . javaBin ;
59
75
} else {
0 commit comments