Home | Gaming | Programming | Play Online | Contact | Keyword Query
Games++ Games & Game Programming

GAMES++
Games++ Home
Games++ Gaming
Games++ Programming
Beta Testing Games
Free Online Games
Hints & Cheats

BROWSER UTILITIES
E-mail This Page
Add to Favorites

SITE SEARCH

Web Games++

AFFILIATES
Cheat Codes
Trickster Wiki
Game Ratings
Gameboy Cheats
PlayStation Cheats
BlackBerry Games
Photoshop Tutorials
Illustrator Tutorials
ImageReady Tutorials

ADVERTISEMENT

ADVERTISEMENT

How To Obtain From Where a Class is Loaded

Java How To: Source Code

public class LoadingFromWhere 
{
	public static void main(String args[])
	{
    		LoadingFromWhere s = new LoadingFromWhere();
		s.doit();
	}

	public void doit() 
	{
		System.out.println(this.getClass().getName() + " is loaded from " +
			getClass().getProtectionDomain().getCodeSource().getLocation());
    
		MyClass s = new MyClass();
	}
}

class MyClass 
{
	MyClass() 
	{
		System.out.println(this.getClass().getName() + " is loaded from " +
			this.getClass().getProtectionDomain()
				.getCodeSource().getLocation());
	}
}

The Output:

>java LoadingFromWhere
LoadingFromWhere is loaded from file:/C:/temp/
MyClass is loaded from file:/C:/temp/

Another Technique

public class FromWhere 
{
	public static void main(String args[])
	{
		Class theClass = java.lang.Object.class;
		java.net.URL u = theClass.getResource("");
		System.out.println("The Java RunTime used is located at : " + u);
    
		theClass = FromWhere.class;
		u = theClass.getResource("");
		System.out.println("This class (FromWhere) is located at : " + u);
	}
}

The Output

> JAVA FromWhere
The Java RunTime used is located at : jar:file:/C:/DEV/java/jdk1.2.1/jre/lib/RT.JAR!/java/lang/
This class (FromWhere) is located at : null

How to Determine The Location of a Class Running From a JAR file

package com.gpp;

public class HelloClass 
{
	public static void main(String[] args) 
	{
		new HelloClass().say();
	}

	public void say() 
	{
		String className = this.getClass().getName().replace('.', '/');
		String classJar = this.getClass().getResource(
			"/" + className + ".class").toString();

		if (classJar.startsWith("jar:")) 
		{
			System.out.println("*** running from jar!");
		}
		System.out.println(classJar);
	}
}

The Output

jar cvfm Hello.jar manifest.mft com\rg
agnon\HelloClass.class
added manifest
adding: com/gpp/HelloClass.class (in=1059) (out=601) (deflated 43%)

java com.gpp.HelloClass
file:/C:/DEV/WORK/JAVA/com/gpp/HelloClass.class

java -jar Hello.jar
*** running from jar!
jar:file:/C:/DEV/WORK/JAVA/Hello.jar!/com/gpp/HelloClass.class

Copyright © 1998-2007, Games++ All rights reserved. | Privacy Policy