0

The project name is 'producer'. I have a file located in project folder C:/Users/Documents/producer/krb5.conf. If I want to write its relative path, should I write

File file = new File("krb5.conf");

or

File file = new File("producer/krb5.conf");

or

File file = new File("./krb5.conf");

?

4
  • 2
    That looks like a resource. If so: Better to load your files/images as resources: technojeeves.com/index.php/aliasjava1/… technojeeves.com/index.php/aliasjava1/… Commented Apr 26, 2022 at 18:08
  • Even though my answer below works, I completely agree with @g00se here. If your krb5.conf file is a static resource, it belongs into the specific resources folder, which then requires some additional effort to load the file into the program. Commented Apr 26, 2022 at 18:13
  • Never concern yourself with your IDE - real software certainly doesn't. You can never tell from which directory real software is run, so if you DO need an actual file rather than a resource, use user.home. For one thing that's really the only place you can be assured of read/write permissions Commented Apr 26, 2022 at 18:17
  • if I want to address the file path as ${user.home}/producer/krb5.conf? @g00se Commented Apr 27, 2022 at 22:39

1 Answer 1

1

You can use both your 1. and 3. option.

The 2. option would refer to C:/Users/Documents/producer/producer/krb5.conf.

For the purpose of testing you could try to get the absolute path from each file and print it.

        // 1.
        File file1 = new File("krb5.conf");
        File file2 = new File("producer/krb5.conf");
        File file3 = new File("./krb5.conf");

        System.out.println(file1.getAbsolutePath());
        // Output: C:\Users\Documents\producer\krb5.conf

        System.out.println(file2.getAbsolutePath());
        // Output: C:\Users\Documents\producer\producer\krb5.conf

        System.out.println(file3.getAbsolutePath());
        // Output: C:\Users\Documents\producer\.\krb5.conf   

The 3. path may look a bit weird at first, but it also works.

C:\Users\Documents\producer\. points to the current directory, so it is essentially the same as C:\Users\Documents\producer.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I have a follow up question. I'm setting proj_dir value in environment variable. currently it is the project path 'C:\Users\Documents\producer'. However, I need to change it to a relative path too because I need to make sure it can run on any laptop not only mine. I tried to set it to proj_dir: {'producer'} but it not works
What you're probably looking for is the System.getenv(String) method, which will return the value of any environment variable you specify. Your code for creating the file object would then be new File(System.getenv("proj_dir") + "krb5.conf")
Is that file meant to be written to?
the krb5.conf is the file app will read

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.