This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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
My goodness!!! What a complex program....Marcus, you are one smart, smart man...!!!
) ) ) )
Joined: Dec 06, 2001
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.
Joined: Dec 24, 2004
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 ]