Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to load class from JAR file Posted by Mr. Ego on 26 Aug 2005 at 10:46 AM
Hello, I want to load a class from a JAR file. It is saved under /mnt/datadisk/java/share, but altough I add it to library path the program fails loading class. here is code I use:
System.setProperty("java.library.path",System.getProperty("java.library.path")+":"+"/mnt/datadisk/java/share");
		System.out.println(System.getProperty("java.library.path"));
	try {
		Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e ) {
		System.out.println("Class not found");
	}

Thanks for any advices

Report
Re: How to load class from JAR file Posted by arb123 on 29 Aug 2005 at 3:20 AM
: Hello, I want to load a class from a JAR file. It is saved under /mnt/datadisk/java/share, but altough I add it to library path the program fails loading class. here is code I use:
System.setProperty("java.library.path",
  System.getProperty("java.library.path")+":"+"/mnt/datadisk/java/share");


Changing this String has no effect (as you have found). Same goes for all the properties stored here (user directory etc).

If you want to load classes dynamically, try java.net.URLClassLoader. Either construct a file:// type url or use the File.toUrl() method. Use the loadClass method to get the java.lang.Class and then use its newInstance method to instantiate it. You can use java.lang.reflect.Constructors if they take arguments.

This method of instantiation is only suitable if the type is in the main classpath (otherwise you'll still get class not found exceptions). But it'll work for JDBC drivers + the like.

---------------------------------
(Its just my sig)
HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.