Skip to content

Commit 3899427

Browse files
committed
Improve support for Java 9+
This avoids the hardcoded message to stderr: [ERROR]: Cannot locate JRE jar in /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home And subsequent exception: java.lang.IllegalArgumentException: info cannot be null at org.fife.rsta.ac.java.JarManager.addClassFileSource(JarManager.java:157) at org.fife.rsta.ac.java.JarManager.addCurrentJreClassFileSource(JarManager.java:193) at org.scijava.ui.swing.script.languagesupport.JavaLanguageSupportPlugin.<init>(JavaLanguageSupportPlugin.java:56) Which happens because the LibraryInfo.getJreJarInfo(File) method fails to locate the main JRE JAR file, because Java 9+ does not have one -- rather, core classes are divided into modules in the jmods folder. And allows the plugin to be created successfully anyway. Fixes #29. It also makes a best effort to find the "main JRE JAR" -- which for the moment we point at jmods/java.base.jmod, one of many core modules -- and the sources archive in its new location, which is lib/src.zip. Unfortunately, this logic is still insufficient to actually allow RSyntaxTextArea's language support to display javadoc for core Java classes with Java 9+, because the src.zip's internal directory structure is now divided by module, which the library does not yet understand. So really, an upstream fix will be needed to address this. Someday.
1 parent e83b2f7 commit 3899427

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

src/main/java/org/scijava/ui/swing/script/languagesupport/JavaLanguageSupportPlugin.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@
3131

3232
package org.scijava.ui.swing.script.languagesupport;
3333

34+
import java.io.File;
3435
import java.io.IOException;
3536

3637
import org.fife.rsta.ac.java.JavaLanguageSupport;
38+
import org.fife.rsta.ac.java.buildpath.JarLibraryInfo;
39+
import org.fife.rsta.ac.java.buildpath.LibraryInfo;
40+
import org.fife.rsta.ac.java.buildpath.ZipSourceLocation;
3741
import org.scijava.plugin.Plugin;
3842
import org.scijava.ui.swing.script.LanguageSupportPlugin;
3943
import org.scijava.ui.swing.script.LanguageSupportService;
@@ -51,14 +55,51 @@ public class JavaLanguageSupportPlugin extends JavaLanguageSupport implements
5155
{
5256

5357
public JavaLanguageSupportPlugin() throws IOException {
54-
super();
55-
56-
getJarManager().addCurrentJreClassFileSource();
58+
final LibraryInfo info = getMainJreJarInfo();
59+
if (info != null) getJarManager().addClassFileSource(info);
5760
}
5861

5962
@Override
6063
public String getLanguageName() {
6164
return "java";
6265
}
6366

67+
// -- Helper methods --
68+
69+
/**
70+
* Replacement for {@link LibraryInfo#getMainJreJarInfo()}, which is smarter
71+
* about Java 9+, and which does not spew messages to stderr.
72+
*/
73+
private static LibraryInfo getMainJreJarInfo() {
74+
String javaHome = System.getProperty("java.home");
75+
return getJreJarInfo(new File(javaHome));
76+
}
77+
78+
/**
79+
* Replacement for {@link LibraryInfo#getJreJarInfo(java.io.File)}, which is
80+
* smarter about Java 9+, and which does not spew messages to stderr.
81+
*/
82+
private static LibraryInfo getJreJarInfo(final File jreHome) {
83+
final File classesArchive = findExistingPath(jreHome, "lib/rt.jar",
84+
"../Classes/classes.jar", "jmods/java.base.jmod");
85+
if (classesArchive == null) return null; // unsupported JRE structure
86+
87+
final LibraryInfo info = new JarLibraryInfo(classesArchive);
88+
89+
final File sourcesArchive = findExistingPath(jreHome, "lib/src.zip",
90+
"lib/src.jar", "src.zip", "../src.zip", "src.jar", "../src.jar");
91+
if (sourcesArchive != null) {
92+
info.setSourceLocation(new ZipSourceLocation(sourcesArchive));
93+
}
94+
95+
return info;
96+
}
97+
98+
private static File findExistingPath(final File baseDir, String... paths) {
99+
for (final String path : paths) {
100+
File file = new File(baseDir, path);
101+
if (file.exists()) return file;
102+
}
103+
return null;
104+
}
64105
}

0 commit comments

Comments
 (0)