0

tl;dr I'm more used to writing command-line scripts that can just output based on the current working directory, so I'm unsure what directory to use for output files in a program that will be launched from a JAR.

Program Description:

My program builds an HTML file from data given to it from the rest of the program, and then is supposed to write it to a file that we'll call "Output.html" for simplicity.

Relevant Code:

public void outputHTML()
{
    String output = buildHTML();
    
    // Expanded to explain my confusion better
    String fileDirectory = ""; // ???
    String fileName = "Output.html";
    
    String fullPath = fileDirectory + "\\" + fileName;
    
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(fullPath)))
    {
        writer.write(output);
        writer.close();
    } catch (IOException e)
    {
        System.out.println("File not found.");
        e.printStackTrace();
    }
}

Problem

I don't know what to put the file directory as. Usually I run my programs from the command line and use ".\\Output.txt" as my output path, but I don't know where to put it if it's being run from a JAR.

The desired file structure is as follows:

  • Encompassing Folder
    • Program.jar
    • output
      • Output.html

Or alternatively (not sure if this makes it easier to understand or harder):

  • main\
    • main\Program.jar
    • main\output\
      • main\output\Output.html

Everything I can find on SE only relates to reading files that are both immutable and internal, but I'm trying to output a non-static file to a location outside of my jar.

Can anyone help with this? Thanks!

Misc Details

I'm using Eclipse without Gradle currently, because I don't know what Gradle is and new things are scary. If this particular problem would be easier to solve with Gradle, let me know and I'll look up more about it.

EDIT:

  • Added syntax highlighting to code block.
  • Formatted everything a bit better
  • Changed title to be more descriptive
ABadHaiku
  • 65
  • 1
  • 6

1 Answers1

0

You can use an absolute path: e.g. fileDirectory = "\\project\\test\\main\\output";
using normal slash should also work even on Windows ("/project/test/main/output")

Or use a relative path - this will start from the current working directory (user directory), the one where the JVM was started in - e.g. fileDirectory = "main\\output";

  • What is the working directory for a jar program anyway? I've only ever thought of working directories being usable in the command line, not when running a program from an executable, etc. – ABadHaiku Feb 09 '21 at 22:39
  • "the one where the JVM was started in" - that is the directory where the `java` or `java.exe` is executed from. You can print the result of `System.getProperty("user.dir")` to see what it is. "running from an executable"? –  Feb 09 '21 at 22:43
  • Ok, so that definitely wouldn't be a good convention for writing output files, right? I'm not trying to write my files to the user's PATH lol. For that last bit, I was (incorrectly?) lumping jar files and executables together, since I've always thought of them as similar. – ABadHaiku Feb 09 '21 at 22:46
  • Now that I think about it, is there a way to get the absolute directory of the jar and jump around using that? – ABadHaiku Feb 09 '21 at 22:49
  • when double-clicking on the JAR normally (system dependent) the directory where the JAR is, is the working directory. To get the absolute path `new File("").getAbsolutePath()` –  Feb 09 '21 at 22:50