This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Help building a JAR file... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help building a JAR file..." Watch "Help building a JAR file..." New topic
Author

Help building a JAR file...

Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
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.


Marcus L´┐Żubli, SCJP 1.4, CLP 5.0, SCWCD 1.4 (preparing)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Making an executable jar file that your students can double click on would probably be a good thing.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
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 ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
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!
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
My goodness!!! What a complex program....Marcus, you are one smart, smart man...!!!


) ) ) )
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Dec 24, 2004
Posts: 116
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help building a JAR file...