jQuery in Action, 3rd edition
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes JAR packaging problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "JAR packaging problem" Watch "JAR packaging problem" New topic

JAR packaging problem

Victor Pan
Ranch Hand

Joined: May 01, 2002
Posts: 36
Now I am packaging my classes to a jar file. But there is a problem puzzling me. It is about accessing db.db file and image files.
When I am editing my file with JBuilder. I access db.db with file path. Just like this:
, the db file is located in the path of class files. the relation in file system is just like this:
The code can locate db file without any problem.
But when I package all classes and db file to a jar file in the same architecture, the problem complain that it can not locate db.db file. But it is exactly in the same package.
What can I do? Please help me.

Java makes world work!!!
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

use the method getResource() to access the db.db in the jar.
I think it is getResource(). I am getting this off the top of my head, but I know that there are threads already here that show you how to use it. Try doing a search and see what you find.

Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Victor Pan
Ranch Hand

Joined: May 01, 2002
Posts: 36
Thank you, Mark.
But I was told that if I access db file in that way, I will never modified the file? Is that true? Now I have put the db file out of jar. I think that will be more flexible on software design thought. Anyway, thank you very much. You are a nice guy.
Adam Till
Ranch Hand

Joined: Oct 11, 2002
Posts: 41
Treasured Peers
I am wrapping everything up for submission, just done my first .jar file (smiles all round) but got an error. The below returns null:-
String helpDir = "help" + File.separator;
URL file = Class.class.getResource(helpDir + "intro.html");
And this stops my JEditorPane displaying its intro page for the help files.
The above line works fine outside of the .jar. Does the whole Fil.seperator bit not work in a .jar, is it a different referencing structure within?
Any help will be financialy rewarded.

Adam Till<br />SCJP SCJD SCWCD
Richard Yip

Joined: Nov 24, 2002
Posts: 11
Hello Victor,
I have the same problem. Been reading through the old threads and hacking through my server code.
Here's how I did.
public class RemoteDataService extends UnicastRemoteObject implements RemoteData {
private Data data;
private LockedRecords lockedRecords = new LockedRecords();

* Constructor for the RemoteDataService object
* @param dbName Description of the Parameter
* @exception RemoteException Description of the Exception
public RemoteDataService(String dbName) throws RemoteException {
try {
data = new Data(getFile(dbName).toString());
catch (IOException e) {

* Gets the file attribute of the RemoteDataService object
* @param filename Description of the Parameter
* @return The file value
* @exception IOException Description of the Exception
private File getFile(String filename) throws IOException {
//create the fullpathname of the database file
String userDir = System.getProperty("user.dir");
String fileSeperator = System.getProperty("file.separator");
String dbName = userDir + fileSeperator + filename;
File file = new File(dbName);
return file;
I didn't use the getResource method. Instead, I try to get the full pathname of the file and create a File object to access the database file.
The only restriction is that both my server.jar and db.db has to be in the same directory when I run the server.
maybe if I set my classpath before starting the server....

Brian Blignaut
Ranch Hand

Joined: Jan 08, 2003
Posts: 61
As far as my understanding goes, the db file is not in your jar? And the user should be able to specify the file to user as part of your applications comman d line parameters. The spec states that you must include you database for submission, but that does not mean that it must be embedded in the client or server jar.
I agree. Here's the link: http://aspose.com/file-tools
subject: JAR packaging problem
It's not a secret anymore!