0

I am seeing different behavior between Windows, Ubuntu, and Manjaro when creating files. Windows and Manjaro correctly are creating files in the relative path of the jar. However, Ubuntu seems to be creating the files at the user.home location instead.

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

The files are being created a couple different ways...

// 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));

All of these files are ending up in the user home in Ubuntu. I've tried the path name plain file.ext and ./file.ext but neither works.

Additional system details:

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)

EDIT:

It seems that this happens when running the jar from double click after making it executable chmod +x myproject.jar. It seems that running the jar via terminal java -jar myproject.jar the files appear in the relative path beside the jar.

Something about the executable/double click is causing them to appear in the user.home

Matthew Wright
  • 1,485
  • 1
  • 14
  • 38
  • 1
    Anyone can set the current directory to any location they want when they run a program. Solution: do not ever make an assumption about the current directory. If you want files in a certain location, create an absolute path to that location. If you want files in the same directory as a program’s .jar… you’re out of luck, because [the CodeSource of a class can be null.](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/security/ProtectionDomain.html#getCodeSource()) I would question whether there’s any real benefit to saving files in the same directory as the .jar anyway. – VGR Jan 12 '21 at 19:07
  • Before knowing the cause was the executable run via double click, I did see this which better describes my issue and mentions CodeSource https://stackoverflow.com/a/6433564/2650847. – Matthew Wright Jan 12 '21 at 19:40

2 Answers2

0

Maybe you are on the wrong current directory. May I ask if you did open the terminal of Ubuntu and run the build commands?

Thang Bui
  • 1
  • 1
  • 3
  • The jar is located in a folder on the desktop `~/Desktop/myproject` and I have the jar executable `chmod +x myproject.jar`. However, running the jar via terminal `java -jar myproject.jar` does actually put them in the relative path. I guess that changes the question. It seems the double click to run a jar is the cause. – Matthew Wright Jan 12 '21 at 18:52
0

new File( "file" ) and new File( "./file" ) both will create the file with the name "file" in the current working directory. What this is depends on various factors, not only on the operating system.

You may call your program (indirectly) through a startup script that sets the current working directory, some operating systems (more specifically, the respective shell) set it either to the program location or to the user's home or use the current directory for it (or think about something else …).

That means that the location for . inside your program depends completely from the execution environment. This in turn means that you should never make any assumptions what this location should be, not even that you can write to it (you may not even be able to read from it).

So yes, your observation is true, but that is expected and well known. And it is no feature (or bug) of Java or any particular JVM, nor is it really an operating system specific, although it may look like.

tquadrat
  • 3,033
  • 1
  • 16
  • 29