Forums Register Login

Problem with Jar file

+Pie Number of slices to send: Send
I have a small application that I have been working on for several months. It works great. Now when I created an executable .jar file for it, it will not load data from a .csv file. I have tried looking into everything I can think if. I checked file paths, etc. I am using OpenCVS to read the csv. Is there an issue with that when you include it into another .jar file? I am trying to log if there is an error but I don't even get an error. I just doesn't run past that statement...any ideas would be greatly appreciated.
+Pie Number of slices to send: Send
So this .csv file... where is it located? In the file system? Inside the jar?
+Pie Number of slices to send: Send
In the file system. Something like:

App Directory
|-- Data (xml file that will be created when the .csv actually loads. Can write .txt file here fine)
|-- Logs (can write here fine)
|-- Out (other output files)
|-- the .csv data file to load
|-- the .jar file

The .jar and the .csv are in the same directory.
+Pie Number of slices to send: Send
Okay, then you're going to have to provide the complete path to the CSV file to be able to open it. You say it's "in the file system"... is it in any special place, like the user's home directory or something? I don't know what "App Directory" means in your diagram.

Also, what's the current working directory when you run the code from the jar file? You might be able to locate the CSV file relative to that.
+Pie Number of slices to send: Send
How are you trying to open the file?
Can you show the code related to OpenCSV being pointed at this file?

Note that when dealing with an application, it normally uses the "Working Directory" to resolve files from.
More often than not this is the Windows "User" folder or similar rather than the directory where the application jar file is installed.

So you can either
- move the data file to the working directory
- specify the full path to your data file (will have to be configured in some way)
- find a way to put the file on the classpath and retrieve it via getResourceAsStream()

+Pie Number of slices to send: Send
Thanks! Here is the code.

+Pie Number of slices to send: Send
Please UseCodeTags (←click this) when posting code, it makes it much easier to read. I've done it for you this time.
+Pie Number of slices to send: Send
Thank you for fixing that. Sorry!

For clarification, the .jar and the .csv are in the exact same directory. I will look at some of these suggestions today, but if you have any more, please let me know.
+Pie Number of slices to send: Send
I am wondering if having spaces in the file name is messing you up. Try:

+Pie Number of slices to send: Send
Have you tried a file chooser to find the CSV file?
+Pie Number of slices to send: Send
I am using it. The location is correct. I am not sure where the issue is. Everything works perfectly when not compiled and run from the .jar.
+Pie Number of slices to send: Send
How about the manifest? Are the "Main-Class" and "Class-Path" properties set correctly?

Henry
+Pie Number of slices to send: Send
Yes, the .jar runs fine. It just cannot access the files or something like that as it won't load the data...still looking into things.
+Pie Number of slices to send: Send
I don't think it is the spaces as I tried generically without them and it runs fine when not running the .jar file.

I got some logging in place for when I am running the .jar version and the full path to the file is not there. That must be the issue. Why would the relative path work when running it via the command line but not as a .jar?
+Pie Number of slices to send: Send
Okay, I don't think it has anything to do with the files or paths. It is the CSVReader itself. If I comment out the reading it will create the txt and xml data files just fine. It is in the reading in of the data. The full file path is there and correct.

Is is in the way the opencsv-2.3.jar is being used by my .jar?
+Pie Number of slices to send: Send
I recommend using the getAbsolutePath() method when reporting a "file not found" or similar error.

That way you know what directory it's looking in for the file.
+Pie Number of slices to send: Send
Well, if it can't find the file then you'll have got an exception.
If CSVReader couldn't read the file (for whatever reason) then you'll have got an exception.

So...how are you handling exceptions?
+Pie Number of slices to send: Send
 

Stephanie Challand wrote:Is is in the way the opencsv-2.3.jar is being used by my .jar?



It could be. However I don't think you told us yet how you set your classpath while running your jar file.
+Pie Number of slices to send: Send
Here is how I am building the .jar file



The openCSV.jar is in the /lib directory when I am building my .jar.

I also found this, that sounds very similar: http://www.java-forums.org/new-java/85801-problem-opencsv-when-creating-jar-file.html, but I am not sure how to fix things if that even is the issue.
+Pie Number of slices to send: Send
Assuming your manifest file contains the correct classpath and stuff, the jar creation looks fine.

The link you provided, the poster got a UnsupportedClassVersionError, is that what you got? Yet I doubt that's the problem for you. OpenCSV 2.3 at least uses Java 6 so the java version is correct.
+Pie Number of slices to send: Send
Thanks for all the help! I think I finally got it. I had to add the classpath to the mainfest file and then include the lib directory in the app directory that will be distributed.
+Pie Number of slices to send: Send
Success After all that trying.
+Pie Number of slices to send: Send
Yes, Thanks for all the help. Java is not my forte. Just working on a project for a small organization.
+Pie Number of slices to send: Send
You're welcome, though I think I provided the least help. You did manage to answer the questions and keep providing information (‍), which is why the whole thing was sorted out.
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1460 times.
Similar Threads
jdbc not connecting when jar double-clicked
jar sub-folder problem
why is log4j not working in executable jar?
Problems loading images from application JAR
jar can't find file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:24:15.