Skip to content

Commit e803ded

Browse files
committed
Allow setting JDK path via a java.home JSON setting.
Update the documentation.
1 parent 4911c80 commit e803ded

File tree

4 files changed

+70
-31
lines changed

4 files changed

+70
-31
lines changed

README.md

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,37 @@ Note: This tool is not compatible with [vim-lsp](https://github.com/prabirshrest
125125

126126
## Usage
127127

128-
The language server will provide autocomplete and other features using:
129-
* .java files anywhere in your workspace
128+
The language server provides autocomplete and other features using:
129+
* Java source files anywhere in your workspace
130130
* Java platform classes
131-
* External dependencies specified using `pom.xml`, Bazel, or [settings](#Settings)
131+
* External dependencies specified using `pom.xml`, Bazel, or via explicit [settings](#Settings)
132132

133133
## Settings
134134

135-
If the language server doesn't detect your external dependencies automatically, you can specify them using [.vscode/settings.json](https://code.visualstudio.com/docs/getstarted/settings)
135+
Generally, the language server infers the location of the JDK and external
136+
dependency jar files. If this process does not work correctly, you can specify
137+
them explicitly
138+
using [.vscode/settings.json](https://code.visualstudio.com/docs/getstarted/settings).
139+
140+
### Location of the JDK
141+
142+
The location of the JDK is determined by reading the JAVA_HOME environment
143+
variable. If JAVA_HOME is unset, then the language server searches operating
144+
system specific locations for a JDK. The JDK location can be explicitly set
145+
with:
136146

137147
```json
138148
{
139-
"java.externalDependencies": [
140-
"junit:junit:jar:4.12:test", // Maven format
141-
"junit:junit:4.12" // Gradle-style format is also allowed
142-
]
149+
"java.home": "/file/path/of/the/jdk"
143150
}
144151
```
145152

146-
If all else fails, you can specify the Java class path and the locations of
147-
source jars manually:
153+
### External dependency jar files
154+
155+
By default the language server infers the Java classpath and finds source code
156+
jars for external dependencies by running Maven or Bazel. The inference
157+
process can be diabled by explicitly specifying classpath. Both paths may be
158+
set explicitly with:
148159

149160
```json
150161
{
@@ -157,6 +168,17 @@ source jars manually:
157168
}
158169
```
159170

171+
External dependencies can also be specified in Maven or Gradle format with:
172+
173+
```json
174+
{
175+
"java.externalDependencies": [
176+
"junit:junit:jar:4.12:test", // Maven format
177+
"junit:junit:4.12" // Gradle-style format is also allowed
178+
]
179+
}
180+
```
181+
160182
You can generate a list of external dependencies using your build tool:
161183
* Maven: `mvn dependency:list`
162184
* Gradle: `gradle dependencies`

src/main/java/org/javacs/Docs.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class Docs {
1414
/** File manager with source-path + platform sources, which we will use to look up individual source files */
1515
final SourceFileManager fileManager = new SourceFileManager();
1616

17-
Docs(Set<Path> docPath) {
18-
var srcZipPath = srcZip();
17+
Docs(Set<Path> docPath, Path javaHome) {
18+
var srcZipPath = srcZip(javaHome);
1919
// Path to source .jars + src.zip
2020
var sourcePath = new ArrayList<Path>(docPath);
2121
if (srcZipPath != NOT_FOUND) {
@@ -31,12 +31,12 @@ public class Docs {
3131
}
3232
}
3333

34-
private static final Path NOT_FOUND = Paths.get("");
34+
static final Path NOT_FOUND = Paths.get("");
3535
private static Path cacheSrcZip;
3636

37-
private static Path srcZip() {
37+
private static Path srcZip(Path javaHome) {
3838
if (cacheSrcZip == null) {
39-
cacheSrcZip = findSrcZip();
39+
cacheSrcZip = findSrcZip(javaHome);
4040
}
4141
if (cacheSrcZip == NOT_FOUND) {
4242
return NOT_FOUND;
@@ -49,15 +49,23 @@ private static Path srcZip() {
4949
}
5050
}
5151

52-
private static Path findSrcZip() {
53-
var javaHome = JavaHomeHelper.javaHome();
52+
private static Path findSrcZip(Path javaHome) {
53+
if (javaHome == NOT_FOUND) {
54+
javaHome = JavaHomeHelper.javaHome();
55+
}
56+
57+
if (javaHome == NOT_FOUND) {
58+
LOG.warning("Couldn't find Java home.");
59+
return NOT_FOUND;
60+
}
61+
5462
String[] locations = {
5563
"lib/src.zip", "src.zip",
5664
};
5765
for (var rel : locations) {
5866
var abs = javaHome.resolve(rel);
5967
if (Files.exists(abs)) {
60-
LOG.info("Found " + abs);
68+
LOG.info("Found src.zip " + abs);
6169
return abs;
6270
}
6371
}

src/main/java/org/javacs/JavaCompilerService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class JavaCompilerService implements CompilerProvider {
2121
// TODO intercept files that aren't in the batch and erase method bodies so compilation is faster
2222
final SourceFileManager fileManager;
2323

24-
JavaCompilerService(Set<Path> classPath, Set<Path> docPath, Set<String> addExports) {
24+
JavaCompilerService(Set<Path> classPath, Set<Path> docPath, Set<String> addExports, Path javaHome) {
2525
System.err.println("Class path:");
2626
for (var p : classPath) {
2727
System.err.println(" " + p);
@@ -34,7 +34,7 @@ class JavaCompilerService implements CompilerProvider {
3434
this.classPath = Collections.unmodifiableSet(classPath);
3535
this.docPath = Collections.unmodifiableSet(docPath);
3636
this.addExports = Collections.unmodifiableSet(addExports);
37-
this.docs = new Docs(docPath);
37+
this.docs = new Docs(docPath, javaHome);
3838
this.classPathClasses = ScanClassPath.classPathTopLevelClasses(classPath);
3939
this.fileManager = new SourceFileManager();
4040
}

src/main/java/org/javacs/JavaLanguageServer.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,25 @@ private JavaCompilerService createCompiler() {
9393
var externalDependencies = externalDependencies();
9494
var classPath = classPath();
9595
var addExports = addExports();
96+
var javaHome = javaHome();
97+
9698
// If classpath is specified by the user, don't infer anything
9799
if (!classPath.isEmpty()) {
98100
javaEndProgress();
99-
return new JavaCompilerService(classPath, docPath(), addExports);
101+
return new JavaCompilerService(classPath, docPath(), addExports, javaHome);
100102
}
101-
// Otherwise, combine inference with user-specified external dependencies
102-
else {
103-
var infer = new InferConfig(workspaceRoot, externalDependencies);
104103

105-
javaReportProgress(new JavaReportProgressParams("Inferring class path"));
106-
classPath = infer.classPath();
104+
// Otherwise, combine inference with user-specified external dependencies.
105+
var infer = new InferConfig(workspaceRoot, externalDependencies);
107106

108-
javaReportProgress(new JavaReportProgressParams("Inferring doc path"));
109-
var docPath = infer.buildDocPath();
107+
javaReportProgress(new JavaReportProgressParams("Inferring class path"));
108+
classPath = infer.classPath();
110109

111-
javaEndProgress();
112-
return new JavaCompilerService(classPath, docPath, addExports);
113-
}
110+
javaReportProgress(new JavaReportProgressParams("Inferring doc path"));
111+
var docPath = infer.buildDocPath();
112+
113+
javaEndProgress();
114+
return new JavaCompilerService(classPath, docPath, addExports, javaHome);
114115
}
115116

116117
private Set<String> externalDependencies() {
@@ -142,6 +143,7 @@ private Set<Path> docPath() {
142143
}
143144
return paths;
144145
}
146+
145147
private Set<String> addExports() {
146148
if (!settings.has("addExports")) return Set.of();
147149
var array = settings.getAsJsonArray("addExports");
@@ -152,6 +154,13 @@ private Set<String> addExports() {
152154
return strings;
153155
}
154156

157+
private Path javaHome() {
158+
if (settings.has("home")) {
159+
return Paths.get(settings.get("home").getAsString());
160+
}
161+
return Docs.NOT_FOUND;
162+
}
163+
155164
@Override
156165
public InitializeResult initialize(InitializeParams params) {
157166
this.workspaceRoot = Paths.get(params.rootUri);

0 commit comments

Comments
 (0)