Re: using an executable jar file in a new java program
(Possible repost on my part, my apologies if this appears
twice.)
jsaucedo@gmail.com wrote:
so i can run this parser i downloaded at
http://htmlparser.sourceforge.net/
using the following
C:\ics\111\htmlParse>java -jar bin/htmlparser.jar "http://www.bbc.com"
however, what i want to do is expand the functionality in my java
program by calling the parser class in the jar executable.
i'm not sure what i should do to have my program below compile with
giving me an error about not being able to find the package.
where i'm confused about
-import statement?
Yes, it is required. (pauses) Was you question different to that?
Maybe you should be more specific, rather than expect your
reader to guess your meaning.
-classpath needed for jar file when I compile?
Again yes.
[ but taking a wild stab at the wider question - 'How?' ]
You can add other archives on the classpath at time of
compilation or execution. The secret is to specify it
in the '-classpath' argument when compiling/running.
The 'classpath' option can be abbreviated to 'cp' on more
modern VM's. Here is how it might look.
CLI>javac -cp .;bin/htmlparser.jar CallsClassInMainArchive.java
The "-cp.;bin/htmlparser.jar" is the part of interest.
-cp
- the classpath - add these places to this invocation
.
- the current directory (assuming your in the same
directory as the Java code you are writing, or at the
'root' package of your code - this will add your code
into this new classpath)
;
- separator for multiple class path entries
bin/htmlparser.jar
- the location of your external archive of interest.
-do i need to unjar the file? thanks.
No. Java tools as well as the classes are well set-up
for getting resources (classes, images, help files..)
directly out of Zip files (a Jar is just a specialised
form of Zip file).
BTW - Please use Upper Case letters at the start of
sentences. It helps the reader to spot where breaks are,
and therefore makes messages easier to understand.
Andrew T.