Manifest file and Classpath issue

S

skydvr

Hello...

I'm an experienced developer who is somewhat new to Java and just
delving into jar files. I'm having an issue with the jar that I could
use some help with...

I'm developing an application and it executes fine in "non-jar" format.
:) It's contained in a package "com.company.dept". My directory
structure is as follows: ("home" is my user home dir)

home - project - classes - com - company - dept - <java class files>
\- data - <project data files>

During development, I was doing the following to run the app:
java -ea -server -classpath ~/project/classes:~/project/data
com/company/dept/MainClass.java <cmd line args>

When the MainClass is run, it loads a data file (which is under the data
dir) that it finds by utilizing
ClassLoader.getSystemClassLoader().getResourceAsStream(<dataFileName>),
which allows me to not specify a full pathname - it should find the file
as long as it resides anywhere within the classpath, and since I add the
"data" dir to the classpath at execution, no problems.

For deployment, I've packaged it as a jar file: "jar cvmf myManifest
~/myApp.jar data/* -C classes com"

Contents of myManifest:
--------------
Main-Class: com.company.dept.MainClass
Class-Path: data

Name: com/company/dept/
Sealed: true
<blank line>
--------------

The "jar" cmd above is run in "project" dir and tells "jar" to grab
everything in the "data" dir and then change to the "classes" dir and
grab the "com" dir and everything under it. So, in the layout of the
jarfile, the "dataFile" in question would be under a "data" dir - not at
the top level. It doesn't seem to work to give the "jar" cmd two
separate "-C" parameters, and having the data file under a "data" dir is
probably preferable.

So, when I try to run the app:
"java -jar myApp.jar"

The result is that my MainClass exits with a NullPointerException
because the InputStream returned by the "getResourceAsStream" call
(noted above) returns null - it must not be able to find the data file.

So, how do I write the manifest file (I assume using the "Class-Path"
line, since the "-classpath" option works when "not in jar mode") in
order for the jvm to "see" the data directory inside the jar file as
part of the class path? The way my manifest is written doesn't seem to
be working, although it doesn't produce an error either.... Using
"myApp.jar" on the "Class-Path" line didn't help.

I apologize for the length - hope I didn't get too mired in unnecessary
detail.

Thanks for any info... Much appreciated.
 
F

Frank Buss

skydvr said:
jarfile, the "dataFile" in question would be under a "data" dir - not at
the top level. It doesn't seem to work to give the "jar" cmd two
separate "-C" parameters, and having the data file under a "data" dir is
probably preferable.

I don't know, if you can use two "-C" parameters, but you can rename the
jar-file to a zip file (because it is the same format) and unzip it to take
a look what was generated.

And try to use getResourceAsStream("/datadir/datafile") (or "/datafile", if
you have packed it in root in the jar).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top