Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAR packaging problem

 
Victor Pan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
\---db.db
\---suncertify
\------server
\------client
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Mark
 
Victor Pan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Yip
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) {
System.out.println(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....

Richard
 
Brian Blignaut
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic