Don't know package of class in a jar using URLClassLoader
Hello,
i want to load a class out of a jar file but the only thing i know is the classname its the same as the jarname. the problem now is i packed the plugin as nice with packages and my Loader won't find the class without package, what can i do to know the package?
i don't have any main method because it's a plugin loading. i know der should be a possibility to loop through all classes?! or even to find a class implementing an interface (but that was something with ServiceLoader)...how can i do it?
for(int i = 0; files != null && i < files.length; i++) {
MyClassLoader pluginLoader = null;
try {
pluginLoader = new MyClassLoader(MyConstants.MODULES_PATH+files);
} catch(MalformedURLException e) {
System.out.println("Exception: " + e.getMessage());
// cut off extension for className
int dot = files[i].lastIndexOf(MyConstants.MODULE_EXTENSION);
String className = files[i].substring(0, dot);
MyPluginInterface plugin = null;
try {
plugin = (MyPluginInterface)pluginLoader.findClass(className).newInstance();
} catch(Exception e) {
System.out.println("Exception: " + e.getMessage());
this works (for one of the jars with this package then):plugin = (MyPluginInterface)pluginLoader.findClass("my.package.in.jar."+className).newInstance();
public class MyClassLoader extends URLClassLoader {
public MyClassLoader (String filenName) throws MalformedURLException {
super(new URL[]{new File(filenName).toURI().toURL()}, MyClassLoader.class.getClassLoader());
public Class<?> findClass(String name) throws ClassNotFoundException {
return super.findClass(name);
thx for you help!
YES! just a bit of work and it's running nice :-)
if(!(Constants.MODULES_PATH.endsWith("/"))) Constants.MODULES_PATH = Constants.MODULES_PATH + "/";
File path = new File(Constants.MODULES_PATH);
if(path.exists() && path.isDirectory()) {
// find all .jar files
String[] files = path.list(new FilenameFilter() {
public boolean accept(File d, String name) { return name.endsWith(Constants.MODULE_EXTENSION); }
for(int i = 0; files != null && i < files.length; i++) {
// create class loader for jar
MySystemClassLoader classLoader = null;
try {
classLoader = new MySystemClassLoader(Constants.MODULES_PATH+files);
} catch(MalformedURLException e) {
System.out.println("Exception: " + e.getMessage());
// load service(s)/module(s) (can be more than one in a jar!)
ServiceLoader<ModuleInterface> moduleLoader = ServiceLoader.load(ModuleInterface.class, classLoader);
for(ModuleInterface module : moduleLoader) {
System.out.println("load module: "+module.getClass().getName()+"!");
}and this as class loader (given to the service loader)public class MySystemClassLoader extends URLClassLoader {
public MySystemClassLoader(String filenName) throws MalformedURLException {
super(new URL[]{new File(filenName).toURI().toURL()}, MySystemClassLoader.class.getClassLoader());
public Class<?> findClass(String name) throws ClassNotFoundException {
return super.findClass(name);
in the jar-file (module/service) you need a file named as the interface (path.to.package.ModuleInterface) inside of a folder "META-INF/services/" and contains just as a string (path.to.package.ModuleImplementation) the name of the implementaion class (in the jar-file)
Edited by: mrp_cologne on Jul 21, 2008 3:55 AM
Similar Messages
-
Please help.
I bought the IMAC 21.5" /2.5QC /2X2GB /500GB /6750M sept2012. After the automatic update run when restart computer the screen is on the gray screen with the logo and wont' do anything else. I tried to reboot holding the shift key without success. The package did not come with a CD so I can not reinstall the previous system. I even don't know what is the system that came with the computer because I just start using it. I bought in the US but am in Brazil. HELP PLEASE, how can I get the previous system so I can reinstall it? I do not mind loosing data but I need to work on it. PLEASE HELP! Thanks!Try booting into the Recovery HD which is on the HD. Restart holding down Command+r and at the screen with four choices select Disk Utility>Macintosh HD and click on 'repair disk'. If any changes are made click on repair disk again until you get a clear pass. Restart as normal from the Apple menu.
More about the Recovery HD:
http://support.apple.com/kb/HT4718 -
How do I create a new Apple ID and delete my current one?
You can't delete an account, all you can do is remove any personal info from in and stop using it - but anything that you downloaded from the stores with it will remain tied to that account.
If you don't know the password for it then try this page to get it reset : http://iforgot.apple.com
If you also don't know your security answers, then if you have a rescue email address (which is not the same thing as an alternate email address) set up on your account then steps 1 to 5 on the page that you posted from should let you reset them : go to https://appleid.apple.com/ and click 'Manage your Apple ID' on the right-hand side of that page and log into your account, then click on 'Password and Security' on the left-hand side of that page and on the right-hand side you should see an option to send security question reset info to your rescue email address.
If you don't have a rescue email address (you won't be able to add one until you can answer 2 of your questions) then you won't get the reset option - you will need to contact iTunes Support / Apple to get the questions reset.
Contacting Apple about account security : http://support.apple.com/kb/HT5699
When they've been reset (and if you don't already have a rescue email address) you can then use the steps half-way down the page that you posted from to add a rescue email address for potential future use.
How to create a new account : http://support.apple.com/kb/HT2731 -
Homework help--almost done but don't know how my class and main work?
hello, i'm doing a project for my intro to java class. i'm almost done but i'm very confused on making my main and class work. the counter for my class slotMachine keeps resetting after 2 increments. also i don't not know how to make my quarters in the main receive a pay out from my class. Please guide me to understand what i have to do to solve this problem...I have listed my question page and my class and main of what i have done so far. Thank you .I will really appreciate the help.
Objective: Create a class in Java, make instances of that class. Call methods that solve a particular problem Consider the difference between procedural and object-oriented programming for the same problem.
Program Purpose:
Find out how long it will take Martha to lose all of her money playing 3 instances of the slot machine class.
Specification: Martha in Vegas
Martha takes a jar of quarters to the casino with the intention of becoming rich. She plays three machines in turn. Unknown to her, the machines are entirely predictable. Each play costs one quarter. The first machine pays 30 quarters every 35th time it is played; the second machine pays 60 quarters every 100th time it is played; the third pays 11 quarters every 10th time it is played. (If she played the 3rd machine only she would be a winner.)
Your program should take as input the number of quarters in Martha's jar (there will be at least one and fewer than 1000), and the number of times each machine has been played since it last paid.
Your program should output the number of times Martha plays until she goes broke.
Sample Session. User input is in italics
How many quarters does Martha have in the jar?
48
How many times has the first machine been played since playing out?
30
How many times has the second machine been played since paying out?
10
How many times has the third machine been played since paying out?
9
Martha plays XXX times
Specific requirements: You must solve this using an object-oriented approach. Create a class called SlotMachine. Think about: What does a SlotMachine Have? What does it do? What information does it maintain? This is the state and behavior of the object. Each instance of this class will represent a single slot machine. Don�t put anything in the class unless it �belongs� to a single machine.
In the main method, create 3 instances of the slot machine to solve the problem in the spec. Play the machines in a loop.
========================================================================
my class
========================================================================
public class SlotMachine{
private int payOut;
private int playLimit;
private int counter;
public SlotMachine (int payOut, int playLimit){
this.payOut = payOut;
this.playLimit = playLimit;
public void setPayOut (int payOut){
this.payOut = payOut;
public int getPayOut(){
return payOut;
public void setPlayLimit (int playLimit){
this.playLimit = playLimit;
public int getPlayLimit(){
return playLimit;
public void slotCounter(){
counter = SavitchIn.readLineInt();
public int game(){
counter++;
if (counter == playLimit){
counter = 0;
return payOut - 1;
return -1; // the game method was edited by my professor but i'm still confused to make it work
=======================================================================
my main
=======================================================================
public class Gametesting{
public static void main(String[]args){
SlotMachine firstSlotMachine = new SlotMachine (2,3);
SlotMachine secondSlotMachine = new SlotMachine (2,3);
SlotMachine thirdSlotMachine = new SlotMachine (2,2);
int quarters;
int playerCount = 0;
System.out.println("How many quarters does Martha have in the jar?");
quarters = SavitchIn.readLineInt();
System.out.println("How many times has the first machine been played since paying out?");
firstSlotMachine.slotCounter();
System.out.println("How many times has the second machine been played since paying out?");
secondSlotMachine.slotCounter();
System.out.println("How many times has the third machine been played since paying out?");
thirdSlotMachine.slotCounter();
while (quarters != 0){
playerCount++;
quarters--;
firstSlotMachine.game();
if (quarters != 0){
playerCount++;
quarters--;
secondSlotMachine.game();
if (quarters != 0){
playerCount++;
quarters--;
thirdSlotMachine.game();
System.out.println("Martha plays " + playerCount + " times");The main problem is that you made the first and second slot machine to pay out 2 quarters after 3 games and the third machine pay out two quarters after two games. That is not what your assignment specified.
SlotMachine firstSlotMachine = new SlotMachine (2,3);
SlotMachine secondSlotMachine = new SlotMachine (2,3);
SlotMachine thirdSlotMachine = new SlotMachine (2,2);
The second problem is that you never add the payout of a machine to Martha's number of quarters.
Look carefully at the way your professor implemented the game() method. If you think about it, what it returns is the net "gain" from playing a game. On jackpot it returns payOut -1 (jackpot less the one quarter Martha had to pay for the game), otherwise it returns -1 (Martha spends one quarter and wins nothing).
So what you can do is simply add the returned value to the number of quarters in the jar -
quarters = <machine>.game();
instead of quarters--. -
Don't know what kind of application I should use to make this kind of proj.
I am going to make a project and I am not sure what kind of program would work for this document I want to make. The project I would like to make is of a type like a family tree, or a reverse sports playoff grid, where you start out with a few headers that branch out laterally and are linked with lines (just like a family tree graph) to many different headers. I want to have each of these headers as hyperlinks that you can click on to read more text/data/pictures about each header. I want to be able to add and edit it after I created it without major adjustments to the whole thing. Is this a clear question? Any help would be great. Thanks.
PowerPC G5 Mac OS X (10.4.2)
PowerPC G5 Mac OS X (10.4.2)Donna,
At a quick look at the omnigraffle webpage and I knew
that was excatly the application I was looking for
but did not know existed or where to find. Thanks a
ton for your input!
Also, it did not come with my prepackaged software.
Mrl
PowerMac G5s sold in the last 2 years or more all came with OmniGraffle.
Apple's G5 web page
If it's not on your G5, check the DVD that came with your G5.
Cheers. -
iTunes no longer can find songs that I purchased from the iTunes store and downloaded.
I am getting this same message. I recently bought a new Macbook Pro and the geeks transferred my files from my mini-mac. Perhaps they did not transfer something critical? I follow the "locate" option when it asks to find my files and I get a file folder that has a red circle with a white bar in the middle.
I have the same issues with my downloaded movies but my apps all seem to be fine.
Even more confusing - it seems to allow random songs to play and download on my Macbook and other devices. But most of my music - whether downloaded from iTunes or my CD collection comes up with that message about the original file.
Should I get new geeks to locate these files on my old mini-mac? -
How can delete iCloud's I'd without pasward
By the Way, if your device's Find iPhone is turned off, then you can delete that ID because all the security measures are based on the Find iPhone.
But you still need to contact with that owner -
I use dial-up, FireFox 5.0, Yahoo addition 1.8. My screen load times have slowed down to a crawl. I have to wait and wait for a page to load. (I know --dial -up) It used to be much faster a month ago before 5.0. Is it FireFox or do I look to Yahoo for the problem? I don't know how to sort it out. Using Vista
I'm having the same problem. Dreamweaver is supper slow. Uploading and downloads from the server... any server I use two different servers. It times out all the time. Menus are slow too.
I'm on a MAC Pro, 6 gigs of ram no issues with anything else. I have talked to Apple they have checked everything for me. They say it's Dreamweaver. I've noticed the Dreamweaver is talking to the FTP even when I'm not doing anything.
I've tried cleaning out my MAC caches, turning off file check and check out. Still really slow. I've worked with Dreamweaver for 6 years never any issues like this before. 18 seconds to upload a 4 kb file. 11 seconds to download the same file. -
Don't know how to use the air print with my ipads.
i just bought an mg3222 printer and i don't know how to figure out how to use air print with my ipad and ipad minis. Can anybody help??? Thanks
Hello.
In order to use the printer with your iOS devices, please make sure that the units are all connected to the same WI-Fi router. Once this has been done you should be able to print.
The initial Wi-Fi setup of the MG3222 would have to be done from a compatible Windows or Mac OSX computer.
If additional assistance is needed, feel free to call us at 1-800-OKCANON.
Did this answer your question? Please click the Accept as Solution button so that others may find the answer as well. -
My firefox program open with other program and i don't know why
when i open firefox program another program (ie8) open in the place of firefox and i don't know why
and also i prefere to use firefox program
please help me
thanks
== This happened ==
Every time Firefox opened
== i open firefox ==
== User Agent ==
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; FunWebProducts; .NET CLR 2.0.50727; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; AskTbGLSV5/5.8.0.12217)Have you read [[Links do not open in Firefox]]
-
It says I have to put a PUK code and I don't know what to put
It says I have to put a PUK code and I don't know what to put
Hello Naly318,
Thanks for using Apple Support Communities.
For more information on this, take a look at:
iOS: Understanding the SIM PIN
http://support.apple.com/kb/ht1316
Personal Unlocking Key (PUK)
If you enter the PIN incorrectly three times, you may need to enter a Personal Unlocking Key (PUK) to enable your SIM card again. Refer to the SIM card documentation or contact your carrier.
Note: Entering an incorrect PUK code too many times will result in the SIM becoming permanently locked. If this occurs, you will need to contact your carrier for a replacement SIM card.
Best of luck,
Mario -
I don't know what program i am supposed to open the Firefox Sync add-on software with
I have been attemtping to download and run the Firefox Sync software for the Firefox mobile app to work with my iPhone4. After downloading the software I keep getting stuck because I don't know what software I'm supposed to use to open/run the download. Can you please advise me and let me know where to get it.
Thanks
DeanTo use Firefox Sync on your iPhone, you will need to download our free "Firefox Home" app from the App Store. For details, see: http://firefox.com/mobile/home/
-
I don't know what's wrong with my code. it's about class and object.
* This is generic type of Person
package myManagement;
* @author roadorange
public class Person {
private String SS;
private String firstName;
private String lastName;
private String middleName;
private String phoneNumber;
private String address;
private String birthDay;
public void setSS (String SS) {
this.SS = SS;
public String getSS() {
return SS;
public void setFirstName (String firstName) {
this.firstName = firstName;
public String getFirstName() {
return firstName;
public void setLastName (String lastName) {
this.lastName = lastName;
public String getLastName() {
return firstName;
public void setMiddleName (String middleName) {
this.middleName = middleName;
public String getMiddleName() {
return middleName;
public void setPhoneNumber (String phoneNumber) {
this.phoneNumber = phoneNumber;
public String getPhoneNumber() {
return phoneNumber;
public void setAddress (String address) {
this.address = address;
public String getAddress() {
return address;
public void setBirthDay (String birthDay) {
this.birthDay = birthDay;
public String getBirthDay() {
return birthDay;
public void Person() {
SS = "1234567890";
this.firstName = "abc"; //test the keyword "this"
this.lastName = "xyz";
middleName = "na";
phoneNumber = "123456789";
address = "11-11 22st dreamcity ny 11111";
birthDay = "11-11-1980";
public void print() {
System.out.println("Display Database\n"
+ "Social Security Number: *********" + "\n"
+ "First Name: " + getFirstName() + "\n"
+ "Middle Name: " + getMiddleName() + "\n"
+ "Last Name: " + getLastName() + "\n"
+ "Phone Number: " + getPhoneNumber() + "\n"
+ "Address: " + getAddress() + "\n"
+ "getBirthDay: " + getBirthDay() );
package myManagement;
//this class is used to test other class or test other object
public class testClass extends Person{
public static void main(String[] args) {
Person obj1 = new Person();
obj1.print();
System.out.println(obj1.getFirstName()); //test the object
Result:
Display Database
Social Security Number: *********
First Name: null
Middle Name: null
Last Name: null
Phone Number: null
Address: null
getBirthDay: null
null
i don't know why it's all null. i assign something in the default constructor. it shouldn't be "null".
does anyone know why?when you create 2 class using netbean editor, person.java and test.java
i never compile person.java.
when i finish typing these 2 class and i just right click test.java and run.
my question is do i need to compile person.java before i run test.java directly??
does Netbean compile it automatically when i run test.java first time?
i add another constructor Person constuctor with one parameter in Person.java and create 2nd object to test the 2nd constuctor and run.
All the codes work, so does the first default constructor. i don't why is that.
Then i removed what i added and restore back to where it didn't work before and test again. it works.
@_@. so weird.
problem solved. thank you
Edited by: roadorange on Feb 25, 2008 7:43 PM -
today in class someone got in my phone and changed the password on it. i don't know what it is, so i have no way of getting into my phone. does anyone know a way i could get into it without knowing the passcode?
See this thread: https://discussions.apple.com/message/17811499#17811499
-
I purchased the package that allows you to export PDF files into Word files. Whenever I try to export the PDF into a Word file it never works... I don't know if there's something key that I'm missing but I'm pretty bummed I paid for it and it won't work. Can anybody help me out?
Hello,
I have paid for this service for a year and it never worked on my computer. I just renewed and it finally converted the file but it will not let me edit it. I join you in being bummed. Either I get help with this or I am asking for a 2 year refund. My intention was to be able to edit a pdf in MS Word.
pfierrorob
Maybe you are looking for
-
When I try and open Microsoft Word 2011 documents on my IMAC they start opening one after another at a rapid speed and I can't stop it. I reboot and same thing happens. The documents open in rapid succession on their own and don't stop until I force
-
Best practices for sharing mailboxes
Hello, To share mailboxes, I am not sure of the 'best practices'. From my point of view, there are two scenarios : classic mailbox : an user A wants to share his mailbox so it assigns rights on his mailbox for an userB via Outlook. The userB will ad
-
Homepage on IPHOTO 6.O.4?
How do I transfer photo's to my homepage. No homepage icon on Iphoto menu bar. What happened to it... I have IWeb but no homepage Icon.
-
Downloaded on a different laptop, wont let me sync
I bought an app on a different laptop, and it wont allow me to sync it onto my ipod, what do i do?
-
Hi Can anyone please help me, im trying to write a jabber client using Smack. I want to send a message to a Smack user via the command line. I understand I need to use BufferedReader but I am unware of the exact syntax. Here is the code: The createMe