Skip to content

Commit cf8601c

Browse files
author
Satyen Subramaniam
committed
8302293: jar --create fails with IllegalArgumentException if archive name is shorter than 3 characters
Backport-of: bd0fde71d9113bad902e71b61f0ca44c650809ef
1 parent 96866ce commit cf8601c

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

src/jdk.jartool/share/classes/sun/tools/jar/Main.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,13 @@ public synchronized boolean run(String[] args) {
323323
// error("Warning: -v option ignored");
324324
vflag = false;
325325
}
326-
final String tmpbase = (fname == null)
326+
String tmpFilePrefix = (fname == null)
327327
? "tmpjar"
328328
: fname.substring(fname.indexOf(File.separatorChar) + 1);
329-
330-
tmpFile = createTemporaryFile(tmpbase, ".jar");
329+
if (tmpFilePrefix.length() < 3) {
330+
tmpFilePrefix = "tmpjar" + tmpFilePrefix;
331+
}
332+
tmpFile = createTemporaryFile(tmpFilePrefix, ".jar");
331333
try (OutputStream out = new FileOutputStream(tmpFile)) {
332334
create(new BufferedOutputStream(out, 4096), manifest);
333335
}
@@ -1745,11 +1747,12 @@ private File createTemporaryFile(String tmpbase, String suffix) {
17451747
// Unable to create file due to permission violation or security exception
17461748
}
17471749
if (tmpfile == null) {
1748-
// Were unable to create temporary file, fall back to temporary file in the same folder
1750+
// We were unable to create temporary file, fall back to temporary file in the
1751+
// same folder as the JAR file
17491752
if (fname != null) {
17501753
try {
17511754
File tmpfolder = new File(fname).getAbsoluteFile().getParentFile();
1752-
tmpfile = File.createTempFile(fname, ".tmp" + suffix, tmpfolder);
1755+
tmpfile = File.createTempFile(tmpbase, ".tmp" + suffix, tmpfolder);
17531756
} catch (IOException ioe) {
17541757
// Last option failed - fall gracefully
17551758
fatalError(ioe);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.io.File;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import java.util.jar.JarFile;
28+
import java.util.spi.ToolProvider;
29+
import java.util.zip.ZipEntry;
30+
31+
import org.junit.jupiter.params.ParameterizedTest;
32+
import org.junit.jupiter.params.provider.ValueSource;
33+
import static org.junit.jupiter.api.Assertions.assertEquals;
34+
import static org.junit.jupiter.api.Assertions.assertNotNull;
35+
36+
/*
37+
* @test
38+
* @bug 8302293
39+
* @summary verify that a JAR file creation through "jar --create" operation
40+
* works fine if the JAR file name is less than 3 characters in length
41+
* @run junit JarCreateFileNameTest
42+
*/
43+
public class JarCreateFileNameTest {
44+
45+
private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
46+
.orElseThrow(() ->
47+
new RuntimeException("jar tool not found")
48+
);
49+
50+
/*
51+
* Launches "jar --create --file" with file names of varying lengths and verifies
52+
* that the JAR file was successfully created.
53+
*/
54+
@ParameterizedTest
55+
@ValueSource(strings = {"abcd", "abc", "ab", "a", "d.jar", "ef.jar"})
56+
void testCreate(final String targetJarFileName) throws Exception {
57+
final Path cwd = Path.of(".");
58+
final Path tmpFile = Files.createTempFile(cwd, "8302293", ".txt");
59+
final String fileName = tmpFile.getFileName().toString();
60+
final int exitCode = JAR_TOOL.run(System.out, System.err,
61+
"--create", "--file", targetJarFileName, fileName);
62+
assertEquals(0, exitCode, "jar command failed");
63+
// verify the JAR file is created and contains the expected entry
64+
try (final JarFile jarFile = new JarFile(new File(targetJarFileName))) {
65+
final ZipEntry entry = jarFile.getEntry(fileName);
66+
assertNotNull(entry, "missing " + fileName + " entry in JAR file " + targetJarFileName);
67+
}
68+
}
69+
}
70+

0 commit comments

Comments
 (0)