1818import com .google .gson .JsonElement ;
1919import com .google .gson .JsonObject ;
2020import java .net .URI ;
21+ import java .nio .file .FileSystemNotFoundException ;
22+ import java .nio .file .Files ;
2123import java .nio .file .Path ;
2224import java .nio .file .Paths ;
2325import java .util .ArrayList ;
@@ -56,6 +58,7 @@ public class NotebookSessionManager {
5658 private static final String CLASS_PATH = "--class-path" ;
5759 private static final String MODULE_PATH = "--module-path" ;
5860 private static final String ADD_MODULES = "--add-modules" ;
61+ private static final String USER_DIR_PROP = "-Duser.dir=" ;
5962
6063 private final Map <String , CompletableFuture <JShell >> sessions = new ConcurrentHashMap <>();
6164 private final Map <String , JshellStreamsHandler > jshellStreamsMap = new ConcurrentHashMap <>();
@@ -80,16 +83,17 @@ private CompletableFuture<JShell> jshellBuilder(String notebookUri, JshellStream
8083 if (prj != null ) {
8184 notebookPrjMap .put (notebookUri , new ProjectContextInfo (prj ));
8285 }
83- return jshellBuildWithProject (prj , streamsHandler );
86+ return jshellBuildWithProject (notebookUri , prj , streamsHandler );
8487 })).exceptionally (throwable -> {
8588 LOG .log (Level .WARNING , "Failed to get project context, using default JShell configuration" , throwable );
86- return jshellBuildWithProject (null , streamsHandler );
89+ return jshellBuildWithProject (notebookUri , null , streamsHandler );
8790 });
8891 }
8992
90- private JShell jshellBuildWithProject (Project prj , JshellStreamsHandler streamsHandler ) {
93+ private JShell jshellBuildWithProject (String notebookUri , Project prj , JshellStreamsHandler streamsHandler ) {
9194 List <String > compilerOptions = getCompilerOptions (prj );
9295 List <String > remoteOptions = getRemoteVmOptions (prj );
96+ setSystemPropertiesForRemoteVm (remoteOptions , notebookUri );
9397
9498 JShell .Builder builder = JShell .builder ()
9599 .out (streamsHandler .getPrintOutStream ())
@@ -99,11 +103,26 @@ private JShell jshellBuildWithProject(Project prj, JshellStreamsHandler streamsH
99103 if (!compilerOptions .isEmpty ()) {
100104 builder .compilerOptions (compilerOptions .toArray (new String [0 ]))
101105 .remoteVMOptions (remoteOptions .toArray (new String [0 ]));
106+ } else if (!remoteOptions .isEmpty ()) {
107+ builder .remoteVMOptions (remoteOptions .toArray (new String [0 ]));
102108 }
103109
104110 return builder .build ();
105111 }
106112
113+ private void setSystemPropertiesForRemoteVm (List <String > remoteOptions , String notebookUri ) {
114+ try {
115+ URI uri = URI .create (notebookUri );
116+ Path parentPath = Path .of (uri ).getParent ();
117+ if (parentPath != null && Files .isDirectory (parentPath )) {
118+ remoteOptions .add (USER_DIR_PROP + parentPath .toString ());
119+ LOG .log (Level .FINE , "Setting user.dir for JShell: {0}" , parentPath );
120+ }
121+ } catch (IllegalArgumentException | FileSystemNotFoundException e ) {
122+ LOG .log (Level .WARNING , "Could not parse notebook URI to set user.dir: " + notebookUri , e );
123+ }
124+ }
125+
107126 public CompletableFuture <JShell > createSession (NotebookDocument notebookDoc ) {
108127 String notebookId = notebookDoc .getUri ();
109128 return createSession (notebookId );
0 commit comments