Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Bundling up external excel file with jar file  RSS feed

 
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a java project which reads data from .xls file when it starts

Problem

I want to build a .jar file that is elf contained, meaning i want to bundle up the .xls file inside the .jar file so that i can share it with others.

Currently when i build the .jar file, it is able to read data from .xls file only if it is in the same directory as .xls file. If it is not in the same directory, i get FileNotFoundExcepton

Question

How can i package the .xls file inside the .jar file?
 
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How you building jar currently?

If you were to use some build automation tool as Maven for example, you could add such file(s) in resources folder, so they would be included to jar as part of defined default build process.
 
yousaf khan
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using intellij idea to build the jar file from project structure --> Artifacts
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yousaf khan wrote:I am using intellij idea to build the jar file from project structure --> Artifacts


Alright. So there is an option (+ file) to add files you wanted to be in JAR. But I guess what you are struggling is to read such file afterwards.

Can you do an exercise by creating a JAR with xls file built into, and later extracting JAR file to verify you indeed have it built into: jar xf filename.jar  <-- so that woud ensure

yousaf khan wrote:I want to build a .jar file that is elf contained

 
yousaf khan
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how to do that.

How about i share the project with you? Its not a big project, see if you can build the jar file?
 
yousaf khan
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the complete project if you are willing to try to build a jar file

https://drive.google.com/drive/folders/1oNUKxKxtO5quPVvm_buMF_WaRmTjfXOQ?usp=sharing
 
Sheriff
Posts: 24291
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yousaf khan wrote:I want to build a .jar file that is elf contained, meaning i want to bundle up the .xls file inside the .jar file so that i can share it with others.

Currently when i build the .jar file, it is able to read data from .xls file only if it is in the same directory as .xls file. If it is not in the same directory, i get FileNotFoundExcepton

Question

How can i package the .xls file inside the .jar file?



The other question you have, although you don't know it yet, is "How can I access the XLS file from inside the JAR file?"

Right now you're clearly trying to access it as a file. But when it's inside a JAR file it isn't a file. It's a resource. So if you do plan to access it from a jar file, you should write code which looks in your class file for a resource. That would look something like this:



However you might have chosen to put the XLS file into a particular folder in the JAR, in which case you'd have to modify the URL that I posted to account for that.

Such code will also work in your development environment, before you go through the process of creating the JAR. (It searches the class path for the resource.) So you ought to write the code that way in the first place, to make sure you have all the fiddly bits right.
 
yousaf khan
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i used



but its throwing NullPointerException.

In what folder should i place the .xls file?
 
yousaf khan
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem fixed.
I put the .xls file in the same folder as the class file that access this .xls file and accessed it as
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!