Reputation: 153
i have a program java swing program that reads a .txt file. When i try to make runnable jar file it cant read it. How can i make a runnable jar for such a program? thanks
Upvotes: 0
Views: 360
Reputation: 8247
File -> Export -> Java -> Runnable JAR file
Make sure you are setting your launch configuration for the .jar
file correctly, and that you are extracting the required libraries into the .jar
as well.
The text file is probably not being pointed to the right location in your source code. I would suggest looking into a JFileChooser
and getting the location of the selected file from that. You could also have the text file pinpointed to one directory. An example of this:
File dir = new File(System.getenv("APPDATA"), "folder");
if (dir.exists() && dir.isDirectory()) File file = new File(dir, "input.txt");
This code sets up a folder in the user's AppData
folder on their computer (Windows). The file created afterwards is then put into that folder. Using code like that, I can now read that file no matter where my .jar
file is placed, as long as input.txt
exists at that location.
Upvotes: 1
Reputation: 1032
In eclipse go to file -> export -> java -> runnable jar file.
As for reading the .txt file, you can either hard code the file location in the code, or you can feed it in as argument through the command line, ie java myProgram.jar myFile.txt
Upvotes: 0