简体   繁体   中英

Export runnable jar with eclipse

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM