Skip to content

Commit 55d947e

Browse files
committed
Added notebook directory as the default current working directory in the notebook
1 parent 667416d commit 55d947e

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/NotebookSessionManager.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class NotebookSessionManager {
5656
private static final String CLASS_PATH = "--class-path";
5757
private static final String MODULE_PATH = "--module-path";
5858
private static final String ADD_MODULES = "--add-modules";
59+
private static final String USER_DIR_PROP = "-Duser.dir=";
5960

6061
private final Map<String, CompletableFuture<JShell>> sessions = new ConcurrentHashMap<>();
6162
private final Map<String, JshellStreamsHandler> jshellStreamsMap = new ConcurrentHashMap<>();
@@ -80,16 +81,17 @@ private CompletableFuture<JShell> jshellBuilder(String notebookUri, JshellStream
8081
if (prj != null) {
8182
notebookPrjMap.put(notebookUri, new ProjectContextInfo(prj));
8283
}
83-
return jshellBuildWithProject(prj, streamsHandler);
84+
return jshellBuildWithProject(notebookUri, prj, streamsHandler);
8485
})).exceptionally(throwable -> {
8586
LOG.log(Level.WARNING, "Failed to get project context, using default JShell configuration", throwable);
86-
return jshellBuildWithProject(null, streamsHandler);
87+
return jshellBuildWithProject(notebookUri, null, streamsHandler);
8788
});
8889
}
8990

90-
private JShell jshellBuildWithProject(Project prj, JshellStreamsHandler streamsHandler) {
91+
private JShell jshellBuildWithProject(String notebookUri, Project prj, JshellStreamsHandler streamsHandler) {
9192
List<String> compilerOptions = getCompilerOptions(prj);
9293
List<String> remoteOptions = getRemoteVmOptions(prj);
94+
setSystemPropertiesForRemoteVm(remoteOptions, notebookUri);
9395

9496
JShell.Builder builder = JShell.builder()
9597
.out(streamsHandler.getPrintOutStream())
@@ -99,11 +101,20 @@ private JShell jshellBuildWithProject(Project prj, JshellStreamsHandler streamsH
99101
if (!compilerOptions.isEmpty()) {
100102
builder.compilerOptions(compilerOptions.toArray(new String[0]))
101103
.remoteVMOptions(remoteOptions.toArray(new String[0]));
104+
} else if (!remoteOptions.isEmpty()) {
105+
builder.remoteVMOptions(remoteOptions.toArray(new String[0]));
102106
}
103107

104108
return builder.build();
105109
}
106110

111+
private void setSystemPropertiesForRemoteVm(List<String> remoteOptions, String notebookUri) {
112+
String parentPath = Path.of(URI.create(notebookUri)).getParent().toString();
113+
if (parentPath != null) {
114+
remoteOptions.add(USER_DIR_PROP + parentPath);
115+
}
116+
}
117+
107118
public CompletableFuture<JShell> createSession(NotebookDocument notebookDoc) {
108119
String notebookId = notebookDoc.getUri();
109120
return createSession(notebookId);

0 commit comments

Comments
 (0)