• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help building a JAR file...

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just downloaded about 4,000 pages of public domain HTML.

In order to display these pages offline, I found some java code on the web which creates a smart browser for this.

I would like to take these four .java files and put them, together with the 4,000 plus HTML pages and put them into a single jar file.

I'd like the students to be able to click on the jar file and open "index.html". Where do I start?

If you would like me to post the source code, let me know.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The trick is to use ClassLoader.getResource() to load the HTML files, rather than FileReader. This method can find a data file that's "near" a loaded class -- whether that class came from the file system or from a JAR file.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Making an executable jar file that your students can double click on would probably be a good thing.
 
Marcus Laubli
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question at the bottom!

Here's the code I'm using:










In main of the Browser class, I have the following line:



I actually want to access index.html inside of BookShelf.jar which has the Literature package in it.

Do I now take the "http..." and just replace it with index.html, or do I have to do this?


[ January 27, 2005: Message edited by: Marcus Laubli ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your solution is close. The only problem is that package names only have meaning when accessing Java classes. They don't mean anything when accessing text files in the jar. This means that you need to do something like

to find the index.html file in the JAR.

HTH

Layne
 
Marcus Laubli
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Layne.

I've tried to change the public static void main method. Here's what I came up with:



It's not allowing me to compile. I wish I understaood why!
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My goodness!!! What a complex program....Marcus, you are one smart, smart man...!!!
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would help if you post the exact compiler error that you get. My best guess is that are trying to create a ClassLoader object with the "new" keyword. There are two problems with this. First, I think the ClassLoader constructor is private. Second, ClassLoader.getResource() is static, so you don't need an instance of the class to call the method anyway.

HTH

Layne
 
Marcus Laubli
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Layne, If I take the new away from the front of the class loader, I get this:

C:\Documents and Settings\Marcus Laubli>javac Literature\Browser.java
Literature\Browser.java:26: non-static method getResource(java.lang.String) cann
ot be referenced from a static context
ClassLoader.getResource( "Docs//index.html" ) ;
^
1 error


Otherwise, if leave the new in front of ClassLoader, this happens:

C:\Documents and Settings\Marcus Laubli>javac Literature\Browser.java
Literature\Browser.java:28: cannot find symbol
symbol : class getResource
location: class java.lang.ClassLoader
new ClassLoader.getResource "Docs//index.html" ) ;
^
1 error




/** Very simplistic "Web browser" using Swing. Supply a URL on the * command line to see it initially, and to set the destination * of the "home" button. * 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/ */



Rose, be careful whom you call smart. If you take a close look at the code above, In the comments, Marty Hall was the one who wrote the code and published it on the web. I just know the functionality I need, searched and found what Marty did, then tried to change it for my use.

I may have been smart to copy, (that's ok, if you find source code on the web, you can use it, but leave the credits in the comments so that the guy/girl who wrote it really gets the credit for the work) I'm trying to compile after changing ONE LINE, and this time I'm stuck!
[ January 28, 2005: Message edited by: Marcus Laubli ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic