Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Class loaders  (Read 1531 times)
0 Members and 1 Guest are viewing this topic.
Offline nardev

Senior Newbie





« Posted 2005-11-12 15:39:19 »

I've had some trouble understanding the class loader stuff. I sort of figured it out, and now i wish to post the following as a resource to people new to Class loaders:

Actually I haven't figured it out whatsoever and this below is related only to WebStart. When you use webstart you access things through JAR's which are placed locally on your machine without you knowing how the computer will rename them. I think, because of that, i used Class Loaders, but they have other very important uses.

To get a brief correct explanation of the main purpose of Class Loaders read few posts below (Jeff).

Using relative or absulte paths is not enough if you want your application to work on other computers/systems. When making an application it is a very good if not necessary practice to use Class loaders. You use them so that your application (code) can locate outside resources such as images, text files and so on whether it is run on Linux, Windows or using WebStart. So whenever you have a part of your code accessing an outside file (for example, a texture JPG somewhere in you package) you need to replace it with something like the following:

       
1  
2  
3  
4  
5  
6  
7  
String file = "package/dir/dir/space.JPG";
        URL url = this.getClass().getClassLoader().getResource(file);
        URLConnection conn = url.openConnection();
        InputStream i = conn.getInputStream();
              Image image = BitmapImage.loadImage(i, file); //for images...with Input stream you can read bytes
//OR
             BufferedReader reader = new BufferedReader(new InputStreamReader(i);//for txt files


*BitmapImage is a not a standard class so ignore it here. It reads the bytes out of an InputStream

So what you have to pay attention to is the following 
1  
this.getClass().getClassLoader().getResource(file);


I've used other ways of getting a hold of the resources but this one worked the best for me. As i understand it, whatever is in the classpath of the class calling this line can be retrived as an URL.

So there it is. I am also beginning to understand this, but i wanted to share the information i gathered with anyone who might need it. Also this is my understanding of ClassLoaders and even though it is very shallow it helped me get my game up using WebStart.

<a href="http://vedsta5.freeserverhost.com/">CroAsteroids</a> - Shoot asteroids while doing math equations.
Offline g666

Junior Duke





« Reply #1 - Posted 2005-11-12 16:10:48 »

I think if you want it to work on all platforms you will have to replace all the "/" with File.separator 's

desperately seeking sanity
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #2 - Posted 2005-11-12 16:58:45 »

Luckily enough, no you don't - it automatically handles that (file separator issues)

malloc will be first against the wall when the revolution comes...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #3 - Posted 2005-11-13 01:54:06 »

on work independently of different file systems folks invented the Class loaders.

Um, no.

Class loaders exist (1) to allow independant name-spaces for Class data (and related resources) for different parts of a Java program, for instance for dynsmically loaded code modules or mobile code such as when using JNI.  and (2) to allow dynamic addition of new class sources to a program at run-time.

Just finding code in a differnt place on differnt file systems cna be done using -classpath on the Java command line.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline nardev

Senior Newbie





« Reply #4 - Posted 2005-11-13 02:10:57 »

on another note sometimes people need simple explanations...i read your answer again and I can wipe my *** with it. I'm sure there are experts here that can use it for other things too but simple english works better for newbies. People that understood what you wrote already know what you tried to say.

<a href="http://vedsta5.freeserverhost.com/">CroAsteroids</a> - Shoot asteroids while doing math equations.
Offline Vorax

Senior Duke


Projects: 1


System shutting down in 5..4..3...


« Reply #5 - Posted 2005-11-13 02:17:14 »

You do know that Jeff is one of the developers of Java and he works for Sun?  -- might want to find a way to extract your foot.

Offline nardev

Senior Newbie





« Reply #6 - Posted 2005-11-13 02:20:14 »

he can be whatever he wants all i know is when i read his explanation it was way above my understanding. And my post here was for peeps like me, who need simple hands on explanations. Am i being clear? I hope so...plus i'm a bit tipsy and arrogance always pissed me off. "Um no..."

You do know that Jeff is one of the developers of Java and he works for Sun right?  -- might want to find a way to extract your foot.

<a href="http://vedsta5.freeserverhost.com/">CroAsteroids</a> - Shoot asteroids while doing math equations.
Offline nardev

Senior Newbie





« Reply #7 - Posted 2005-11-13 02:33:32 »

i'll gladly modify my post if senor Jeff could support a down to earth explanation of Class loaders.

<a href="http://vedsta5.freeserverhost.com/">CroAsteroids</a> - Shoot asteroids while doing math equations.
Offline Jeff

JGO Coder




Got any cats?


« Reply #8 - Posted 2005-11-13 03:54:47 »

Look at the PlugIn mechanism in JInput (its actually in JUtils if your looking for the code).  It uses dynamic class loaders to load the plug-ins and prevent clash over class names and DLLs.  This way if I name my  Plugin  "Plugin.java" and you name yours "Plugin.java" they can both operate within the same program. The same goes for 2 DLLs both named "pluginjni.dll".  Using  independant ClassLoaders with differnt classpaths assures that each plugin loads and links to the right DLL.  Furthermore, one plugin can actually use a differnt version of the SAME utility class another plugin does and they will each reference and access the right classes, which can both exist simultaneously within the application.

Any container system, such as an EJB or Servlet server,  uses class loaders in order to isolate the seperate applications running within the container.

If you need for any reason to reload classes during run-time, you will also need to isolate those classes in a seperate class loader and create a new class loader to load the new versions of the classes. 

When you make a JNI call, the code for passed classes must be loaded by the receiever. Again, this is done with a sepreate class loader in order to avoid a clash with the names and versions of local classes.

Formally, a ClassLoader provides a unique Class name-space associated with a classpath that describes where new classes are loaded from in order to be added to that name space.  Thats the definition in proper computer science terms.

If you still don't get it, then frankly you probably don't NEED to get it yet.  When you need the facilities to solve a real-world problem, it will all become clear to you.

Now as to your attitude...

Ignorance is fine, my friend, thats what Clueless Newbies is for.

Aggressive ignorance though is the pen-ultimate height of stupidity.  (Barely superceeded, perhapse, by spelling flames.)

When you wish to learn, by all means come back with your questions.  Until then, this really isnt worth any more of my time.  I have corrected your misstatement to prevent you from confusing others and thats all I feel a need to do.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #9 - Posted 2005-11-13 04:45:27 »

By the way.

getClass().getResourceAsStream(<filename>) 

Is the way to get at your resources in a totally installation independant manner.

Thats all you need do so long as the resource is in your classpath or bootclasspath. Also, this formulation will work as written  in MIDP wheras getClass().getClassLoader() will not because MIDP does not support dynamic ClassLoaders.

(It gets a bit trickier in an app that actually uses dynamic ClassLoaders.  In that case the resource is returned if it is reachable by the
ClassLoader that loaded the class of which the invoker is an instance (the Class returned by getClass()) OR any of its parent ClassLoaders.  Prefernce is given to parents first, and the bootclasspath loader is always the root loader with the classpath loader being next down the tree.)



Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nardev

Senior Newbie





« Reply #10 - Posted 2005-11-13 09:50:59 »

I'm sure you'll come back to read my response. After all you are human. Although when i read your explanations i start to wonder Smiley

If my english serves me well, Newless Clubies is for n00b's mostly? I think i also mentioned in my post on top that it is MY understanding of Class Loaders and it might not be correct (I used it for WebStart, jars...). I knew i had it somewhat wrong but i hoped people will correct me. As you did. With a little spice of "Um no". Smiley I hope we all learned something from this. I did. So as i see class loading is way out there for Java experts, complex applications, and i trust that, as you said jefe, i will really learn it once i really need it. At the moment i need it for Web Start as you probably figured out. I will update my first post and hope it helps someone.

<a href="http://vedsta5.freeserverhost.com/">CroAsteroids</a> - Shoot asteroids while doing math equations.
Offline Jeff

JGO Coder




Got any cats?


« Reply #11 - Posted 2005-11-13 21:42:20 »

Your biggest mistake was starting out with the statement "This is what class loaders are for."
That was totally incorrect and neded correction because, as you say, we get a lot of newbs reading these pages.  We want to de-confuse them as much
as possible, not the other way around Cool

You got a short, brusque correction there the first time around because, to be pefectly frank, I'm a  hugely busy guy right now.  That will happen 'round here from time to time as none of us have answering questiosn hre as our primary (or even paid at all) job.

I generally try to take more time to answer when folks ask specific questions because I know then the time spent is of value.

Leave out classloaders, which as I hope I explained are really an entirely different subject, and you werent that far off from what you were really reaching for, which is just a way to load data in an installation independant manner.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (33 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (58 views)
2014-10-16 15:22:06

Norakomi (47 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!