有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

正在用户处创建的java文件。在Ubuntu中使用home而不是相对路径

我看到了Windows、Ubuntu和Manjaro在创建文件时的不同行为。Windows和Manjaro正确地在jar的相对路径中创建文件。然而,Ubuntu似乎是在user.home位置创建文件

File file = new File("file.ext");

这些文件是通过几种不同的方式创建的

// 1 Normal
try (FileOutputStream fos = new FileOutputStream(file);
     OutputStreamWriter writer = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {}
// 2 ImageIO
BufferedImage bufferedImage = ImageIO.read(new URL(imageUrl));
imgFile.createNewFile();
ImageIO.write(bufferedImage, "jpg", imgFile);
// 3 Log4J
<RollingFile name="RollingFile" fileName="./log/output-${date:yyyyMMdd}.log" filePattern="./log/output-%d{yyyyMMdd}.log">
// 4 Sqlite
Connection sqlite = DriverManager.getConnection(String.format("jdbc:sqlite:%s", dbFileName));

所有这些文件最终都会出现在Ubuntu的用户主页中。我尝试过路径名plainfile.ext./file.ext,但都不起作用

其他系统详细信息:

Ubuntu Desktop 20.04
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (build 1.8.0_265-8u265-bo1-6ubuntu2~20.04-bo1)
OpenIDK 64-Bit Server VM (build 25.265-bo1, mixed mode)

编辑:

在使jar可执行chmod +x myproject.jar之后,通过双击运行jar时,似乎会发生这种情况。似乎通过终端java -jar myproject.jar运行jar时,文件会出现在jar旁边的相对路径中

有关可执行文件/双击的某些信息导致它们出现在user.home


共 (1) 个答案

  1. # 1 楼答案

    也许你在错误的当前目录上。请问您是否打开了Ubuntu的终端并运行了构建命令