Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3362,7 +3362,19 @@ private boolean cmdSave(String rawargs) {
// error occurred, already reported
return false;
}
try (BufferedWriter writer = Files.newBufferedWriter(toPathResolvingUserHome(filename),
// Create missing parent directories before writing to target file
Path target;
try {
target = toPathResolvingUserHome(filename);
Path parent = target.getParent();
if (parent != null) {
Files.createDirectories(parent);
}
} catch (Exception e) {
errormsg("jshell.err.file.exception", "/save", filename, e);
return false;
}
try (BufferedWriter writer = Files.newBufferedWriter(target,
Charset.defaultCharset(),
CREATE, TRUNCATE_EXISTING, WRITE)) {
if (at.hasOption("-history")) {
Expand Down
5 changes: 4 additions & 1 deletion test/langtools/jdk/jshell/ToolBasicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ public void testSave() throws IOException {
Compiler compiler = new Compiler();
Path path = compiler.getPath("testSave.repl");
{
Path pathWithDirectories = compiler.getPath("what/ever/testSave.repl");
List<String> list = Arrays.asList(
"int a;",
"class A { public String toString() { return \"A\"; } }"
Expand All @@ -593,9 +594,11 @@ public void testSave() throws IOException {
(a) -> assertVariable(a, "int", "a"),
(a) -> assertCommand(a, "()", null, null, null, "", ""),
(a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"),
(a) -> assertCommand(a, "/save " + path.toString(), "")
(a) -> assertCommand(a, "/save " + path.toString(), ""),
(a) -> assertCommand(a, "/save " + pathWithDirectories.toString(), "")
);
assertEquals(list, Files.readAllLines(path));
assertEquals(list, Files.readAllLines(pathWithDirectories));
}
{
List<String> output = new ArrayList<>();
Expand Down