简体   繁体   中英

How do I properly export a runnable jar?

About an hour or so ago I exported a different program into a runnable jar and it worked perfectly. Now when I right click export on my project and do everything else necessary all I get is the window of my program, when I type things in and press enter none of the code runs. My project has 2 classes and 1 text file that it reads. So I did the export with libraries required into a folder and nothing is in the file. Is it because it reads from a text file? is the text file not packaging with the jar?

I think you want to use something like that so that you read the file from the jar. This seems also helpful. Otherwise you can use the absolute path in your file system, but then you will need to place the file in the same place on a different system, if you ever want to run your program there.

The simplest thing to do is

InputStream inputStream = MyClass.class.getResourceAsStream("MyData.txt");

as it was suggested in the second link. It assumes that you have your file in the root directory of the jar under the name MyData.txt . You can open the jar with an archieve manager (winRAR, xarchiver, etc) and see that the file is there, if you have any doubts.

See also this for a complete example (don't pay too much attention to the class loader part - you should be ok with or without it).

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