0

I have to write a Java Project with a Swing GUI and some smaller formal requirements like a button which does something or methods which implement something, very basic. I have written a programm with a basic GUI including a Jtable which stores information about students(name,semester...) in a Java File. The Problem is that the path of this java-File in my programm is relative, in my case:

public static final String pfad = "src/abgabe/";

public static final String name = "Uni";

File file = new File(pfad + name)

If I export my Programm to JAR and send it around, it wont work on other computers because only I have the Java File.

Can I change my code to make the Jar File creates a new Java file in its own root directory?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Spacefish
  • 305
  • 4
  • 11

1 Answers1

2

Can I change my code to make the Jar File creates a new Java file in its own root directory?

Even if you can, you shouldn't. Most OS manufacturers have been saying for years that application data should not be stored in the apps. installation directory.

Instead put the file in a sub-directory of user.home. That will be a path that:

  • Is reproducible.
  • The app. should have write permissions for.

Although the question is not directly related, the answer is, so see also How can an app use files inside the JAR for read and write?

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Alright, i was able to get the(hopefully general) home directory by using System.getProperty("user.home"). I'll try the JAR File on another Computer and see if it works, thank you for the quick help . – Spacefish Jul 26 '15 at 18:21