0

I don't understand how Java jar files work. I am trying to understand what is possible and not possible when creating a Java jar file. Is it possible to have a String path running normally in a Java jar file? Will this normally work as it works when running main class in eclipse? I mean, I have an absolute path in my main class that grabs the file and reads from it.

public static final String file1 = "C:\\Users\\Documents\\test1.txt";
public static final String file2 = "C:\\Users\\Documents\\test2.txt";

This is what I have when running my program and it works fine. This is inside a class that is called somewhere along when I want to read a file. My question is... will this prevent my jar file from working properly normally AS when running the main class from eclipse?

I have the jar file but what if it doesn't or does it still look for file1 and file2?

3
  • A jar file is just a collection of .class files with some metadata, not all that different from a tar file. The only magic is when they're executable jars -- because you've told it what class contains your main. Commented Nov 25, 2019 at 17:51
  • My mistake. I am sorry for missing the \. Commented Nov 25, 2019 at 17:57
  • The host system's file system is independent of how the Java code is (or is not) packaged. Commented Nov 25, 2019 at 18:26

2 Answers 2

2

It doesn't matter whether that code is in a jar file or not. The strings will still be exactly as they are, and if you pass them to methods that look for files with those paths, it'll look for files with those paths in the file system of the machine where the code is running. It won't look for them inside the jar file.

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

3 Comments

Let me say this if I understand. So, in creating a jar file the path will no be affected regardless. It should just exist and it should work, correct? Meaning this path must exist always no matter in what computer this jar is running, correct?
@Woods - Right. So you probably don't want to do that, you probably want to read the path from some configuration file or similar, so the user is in control.
Awesome! This help me to figure what I was missing in jar file.
0

A Jar file is basically an executable of your project, it is used for example by frontend's who need a backend but don't want to open an IDE for compiling and executing purposes. Your Jar file contains .class files responsible for the execution of your project, you an execute your jar in a server too, so your application will run for more people (if you configure right).

Comments

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.