Using Class and Constructor to create instances on the fly

hi,
i want to be able to create instances of classes as specified by the user of my application. i hold the class names as String objects in a LinkedList, check to see if the named class exists and then try and create an instance of it as follows.
the problem im having is that the classes i want to create are held in a directory lower down the directory hierarchy than where the i called this code from.
ie. the classes are held in "eccs/model/behaviours" but the VM looks for them in the eccs directory.
i cannot move the desired classes to this folder for other reasons and if i try to give the Path name to Class.forName() it will not find them. instead i think it looks for a class called "eccs/model/behaviours/x" in the eccs dir and not navigate to the eccs/model/behaviours dir for the class x.
any ideas please? heres my code for ye to look at in case im not making any sense:)
//iterator is the Iterator of the LinkedList that holds all the names of the
//classes we want to create.
//while there is another element in the list.
while(iterator.hasNext())
//get the name of the class to create from the list.
String className = (String) iterator.next();
//check to see if the file exists.
if(!doesFileExist(className))
System.out.println("File cannot be found!");
//breake the loop and move onto the next element in the list.
continue;
//create an empty class.
Class dynamicClass = Class.forName(className);
//get the default constructor of the class.
Constructor constructor = dynamicClass.getConstructor(new Class[] {});
//create an instance of the desired class.
Behaviour beh = (Behaviour) constructor.newInstance(new Object[] {});
private boolean doesFileExist(String fileName)
//append .class to the file name.
fileName += ".class";
//get the file.
File file = new File(fileName);
//check if it exists.
if(file.exists())
return true;
else
return false;
}

ok ive changed it now to "eccs.model.behaviours" and it seems to work:) many thanks!!!
by the following you mean that instead of using the method "doesFileExist(fileName)" i just catch the exception and throw it to something like the System.out.println() ?
Why don't you simply try to call Class.forName() and catch the exception if it doesn't exist? Because as soon as you package up your class files in a jar file (which you should) your approach won't work at all.i dont think il be creating a JAR file as i want the user to be able to create his/her own classes and add them to the directory to be used in the application. is this the correct way to do this??
again many thanks for ye're help:)

Similar Messages

  • Can I use classes and objects to create my own package in LabVIEW?

    Hi....I am writing my thesis on develpoing a simulation package.  I am trying to compare simulation packages and their feautures as a scope of my project.  Does anyone know if LabVIEW allows users to create their own toolboxes like MATLAB?...i mean something that can be done in object-oriented programming by making classes and objects.
    thanks guys 

    The short answer is yes. NI sells toolkits. There's also OpenG. How you code the guts is up to you - you can do it non-LVOOP or LVOOP.

  • How to Create Instances in the Transient Root Node and Sub Nodes from Root Node Query Method ?

    Hi All,
    I am Creating a BOPF BO with 3 Nodes,
    Node 1) ROOT -- > contains a query,
    using Root Node I have created Search UIBB Configuration in FBI.
    In testing -- > when i enter Data in the Search Criteria Fields am able to get the details in to the query method
    from Imporing parameter : 'IT_SELECTION_PARAMETERS'.
    HERE I am fetching data from a standard table and trying to fill the data in the Node : 'MNR_SEARCH_RESULT'.
    How to Append data to the Sub node 'MNR_SEARCH_RESULT' when there is no Node instance created in the ROOT Node ?
    For This  I have created an instance in the ROOT Node and Using that I tried to create Instance in the Sub Node 'MNR_SEARCH_RESULT'.
    Below is my code which i have placed in the Query method ..
    DATA : LR_DATA TYPE REF TO ZBO_S_ROOT1.
    DATA : LR_SEARCH_RES TYPE REF TO ZBO_S_MNR_SEARCH_RESULT.
    DATA : LO_CI_SERVICE_MANAGER TYPE REF TO /BOBF/IF_TRA_SERVICE_MANAGER,
            LO_TRANSACTION_MANAGER TYPE REF TO /BOBF/IF_TRA_TRANSACTION_MGR.
       LO_CI_SERVICE_MANAGER = /BOBF/CL_TRA_SERV_MGR_FACTORY=>GET_SERVICE_MANAGER( IV_BO_KEY = ZIF_BO_TEST_PO_C=>SC_BO_KEY ).
       LO_TRANSACTION_MANAGER = /BOBF/CL_TRA_TRANS_MGR_FACTORY=>GET_TRANSACTION_MANAGER( ).
    CREATE DATA LR_DATA.
    LR_DATA->KEY = LO_CI_SERVICE_MANAGER->GET_NEW_KEY( ).
    LR_DATA->ROOT_KEY   = IS_CTX-ROOT_NODE_KEY.
    LR_DATA->PIPO_MAT_ID = '100100'.
    LR_DATA->PIPO_MAT_DESC = 'MATERIAL'.
    LR_DATA->PIPO_SPRAS = 'E'.
    LR_DATA->PIPO_MATL_TYPE = 'ZPMI'.
    LR_DATA->PIPO_MATL_GROUP = 'ZKK'.
         DATA lt_mod      TYPE /bobf/t_frw_modification.
         DATA lo_change   TYPE REF TO /bobf/if_tra_change.
         DATA lo_message  TYPE REF TO /bobf/if_frw_message.
         FIELD-SYMBOLS: <ls_mod> LIKE LINE OF lt_mod.
        APPEND INITIAL LINE TO lt_mod ASSIGNING <ls_mod> .
        <ls_mod>-node        =   ZIF_BO_TEST_PO_C=>sc_node-ROOT.
        <ls_mod>-change_mode = /bobf/if_frw_c=>sc_modify_create.
        <ls_mod>-key         = LR_DATA->KEY.
        <ls_mod>-data        = LR_DATA.
    DATA : LT_CHG_FIELDS TYPE /BOBF/T_FRW_NAME.
    DATA : LS_CHG_FIELDS LIKE LINE OF LT_CHG_FIELDS.
    DATA : LV_KEY TYPE /BOBF/CONF_KEY.
    CALL METHOD IO_MODIFY->CREATE
       EXPORTING
         IV_NODE            = ZIF_BO_TEST_PO_C=>sc_node-ROOT
         IV_KEY             = LR_DATA->KEY
         IS_DATA            = LR_DATA
         IV_ROOT_KEY        = IS_CTX-ROOT_NODE_KEY
       IMPORTING
         EV_KEY             = LV_KEY .
    CREATE DATA LR_SEARCH_RES.
    LR_SEARCH_RES->KEY           = LO_CI_SERVICE_MANAGER->GET_NEW_KEY( )..
    LR_SEARCH_RES->PARENT_KEY    = LV_KEY.
    LR_SEARCH_RES->ROOT_KEY    = LV_KEY.
    LR_SEARCH_RES->MATNR    = '123'.
    LR_SEARCH_RES->ERSDA    = SY-DATUM.
    LR_SEARCH_RES->ERNAM    = SY-UNAME.
    **LR_SEARCH_RES->LAEDA    = .
    **LR_SEARCH_RES->AENAM    = .
    **LR_SEARCH_RES->VPSTA    = .
    *LR_SEARCH_RES->LVORM    = .
    LR_SEARCH_RES->MTART    = 'ZPI'.
    LR_SEARCH_RES->MBRSH    = 'ZTP' .
    LR_SEARCH_RES->MATKL    = 'MAT'.
    **LR_SEARCH_RES->BISMT    = ''
    **LR_SEARCH_RES->MEINS    =
    CALL METHOD io_modify->create
               EXPORTING
                 iv_node            = ZIF_BO_TEST_PO_C=>sc_node-MNR_SEARCH_RESULT
                 is_data            = LR_SEARCH_RES
                 iv_assoc_key       = ZIF_BO_TEST_PO_C=>sc_association-root-MNR_SEARCH_RESULT
                 iv_source_node_key = ZIF_BO_TEST_PO_C=>sc_node-root
                 iv_source_key      = LV_KEY
                 iv_root_key        = LV_KEY.
    I am Unable to set data to the Node . I did not get any error message or Dump while executing . when i tried to retrive data I got the details from the node but am unable to view those details in the FBI UI and BOBT UI while testing .
    Please provide your valuable Suggestions.
    Thanks in Adv.
    Thanks ,
    Kranthi Kumar M.

    Hi Kranthi,
    For your requirement you need only two nodes. Root Node and Result node. Use the same structure for both.
    To create Instance while search.
    Create Query method with input type which has the required fields for selection criteria.
    Fetch the data and create instance in the root node.
    Pass the new instance key as exporting parameter form Query Method.
    To Move data from ROOT to Result.
    Create a action at root node.
    Write a code to create new entries in Result node.
    Then configure the Search UIBB and display result in List UIBB. Add button and assign the action MOVE_MAT_2_RESULT.
    Create another List uibb to display data from Result node.
    Connect the UIBBs using wire schema. SEARCH -> LIST(ROOT) ---> LIST(RESULT).
    Give src node association for ROOT to RESULT Configuration.
    Regards,
    Sunil

  • List display for ALV using class and methods

    Hi friends
    I want the list display for the ALV using Class and methods
    which class and methods i can use.
    Here we can't use the REUSE_ALV_LIST_DISPLAY and also GRID
    I was done GRID display using class and methods but i want only list display for using class.
    plz Give me sample code of list display not for grid.
    Thanks
    Nani.

    hi
    please check with this code...
    declare grid and container.
    DATA : o_alvgrid TYPE REF TO cl_gui_alv_grid,
    o_dockingcontainer TYPE REF TO cl_gui_docking_container,
    i_fieldcat TYPE lvc_t_fcat,"fieldcatalogue
    w_layout TYPE lvc_s_layo."layout
    If any events like double click,etc., are needed we have to add additional functionality.
    call the screen in program.
    Then , create the container as follows
    IF cl_gui_alv_grid=>offline( ) IS INITIAL.
    CREATE OBJECT o_dockingcontainer
    EXPORTING
    ratio = '95'
    EXCEPTIONS
    cntl_error = 1
    cntl_system_error = 2
    create_error = 3
    lifetime_error = 4
    lifetime_dynpro_dynpro_link = 5
    others = 6.
    ENDIF.
    CREATE OBJECT o_alvgrid
    EXPORTING
    i_parent = o_dockingcontainer.
    Build the fieldcatalog
    create a output structure in SEll for the ALV output
    CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
    i_structure_name = <alv output>
    CHANGING
    ct_fieldcat = i_fieldcat[]
    EXCEPTIONS
    inconsistent_interface = 1
    program_error = 2
    OTHERS = 3.
    IF sy-subrc <> 0.
    MESSAGE i030."Error in building the field catalogue
    LEAVE LIST-PROCESSING.
    ENDIF.
    *If you need to modify the field catalog,modify it using field sysmbols
    *setting the layout
    w_layout-grid_title = title.
    w_layout-zebra = 'X'.
    then displaying the output
    CALL METHOD o_alvgrid->set_table_for_first_display
    EXPORTING
    i_save = 'A'
    is_layout = w_layout
    CHANGING
    it_outtab = i_output[]
    it_fieldcatalog = i_fieldcat[]
    EXCEPTIONS
    invalid_parameter_combination = 1
    program_error = 2
    too_many_lines = 3
    OTHERS = 4.
    IF sy-subrc <> 0.
    MESSAGE i032 ."Error in Displaying
    LEAVE LIST-PROCESSING.
    ENDIF.
    *After that in PAI of the screen, you need to free the *object while going back from the screen(according to *your requirement)
    MODULE user_command_9001 INPUT.
    CASE sy-ucomm.
    WHEN 'EXIT' OR 'CANC'.
    PERFORM f9600_free_objects:
    USING o_alvgrid 'ALV' text-e02,
    USING o_dockingcontainer 'DOCKING'
    text-e01.
    LEAVE PROGRAM.
    ENDCASE.
    ENDMODULE. " USER_COMMAND_9001 INPUT
    *in the program, write the follwoing code
    FORM f9600_free_objects USING pobject
    value(ptype)
    value(ptext).
    DATA: l_objectalv TYPE REF TO cl_gui_alv_grid.
    CASE ptype.
    WHEN 'ALV'.
    l_objectalv = pobject.
    IF NOT ( l_objectalv IS INITIAL ).
    CALL METHOD l_objectalv->free
    EXCEPTIONS
    cntl_error = 1
    cntl_system_error = 2
    OTHERS = 3.
    CLEAR: pobject, l_objectalv.
    PERFORM f9700_error_handle USING ptext.
    ENDIF.
    WHEN 'DOCKING'.
    DATA: lobjectdock TYPE REF TO cl_gui_docking_container.
    lobjectdock = pobject.
    IF NOT ( lobjectdock IS INITIAL ).
    CALL METHOD lobjectdock->free
    EXCEPTIONS
    cntl_error = 1
    cntl_system_error = 2
    OTHERS = 3.
    CLEAR: pobject, lobjectdock.
    PERFORM f9700_error_handle USING ptext.
    ENDIF.
    WHEN 'CONTAINER'.
    DATA: lobjectcontainer TYPE REF TO cl_gui_container.
    lobjectcontainer = pobject.
    IF NOT ( lobjectcontainer IS INITIAL ).
    CALL METHOD lobjectcontainer->free
    EXCEPTIONS
    cntl_error = 1
    cntl_system_error = 2
    OTHERS = 3.
    CLEAR: pobject, lobjectcontainer.
    PERFORM f9700_error_handle USING ptext.
    ENDIF.
    WHEN OTHERS.
    sy-subrc = 1.
    PERFORM f9700_error_handle USING
    text-e04.
    ENDCASE.
    ENDFORM. " f9600_free_objects
    FORM f9700_error_handle USING value(ptext).
    IF sy-subrc NE 0.
    CALL FUNCTION 'POPUP_TO_INFORM'
    EXPORTING
    titel = text-e03
    txt2 = sy-subrc
    txt1 = ptext.
    ENDIF.
    endform.
    also check with this
    http://help.sap.com/printdocu/core/Print46c/en/data/pdf/BCSRVALV/BCSRVALV.pdf
    Hope this helps
    if it helped, you can acknowledge the same by rewarding
    regards
    dinesh

  • Can I use classes and methods for a maintenance view events?

    Hello experts,
    Instead of perform/form, can I instead use classes and methods, etc for a given maintenance view event, lets say for example I want to use event '01' which is before saving records in the database. Help would be greatly appreciated. Thanks a lot guys!

    Hi viraylab,
    1. The architecture provided by maintenance view
       for using EVENTS and our own code inside it -
       It is provided using FORM/PERFORM
       concept only.
    2. At this stage,we cannot use classes.
    3. However, inside the FORM routine,
       we can write what ever we want.
       We can aswell use any abap code, including
       classes and methods.
      (But this classes and methods won't have any
       effect on the EVENT provided by maintenance view)
    regards,
    amit m.

  • Standard SAP Reporting Using Classes and Characterstics

    Hi Experts,
    I am currently interested in if there are any options to using classes and characteristics within standard reporting in SAP ECC.  I know some reports have the selection critiera and allow for other fields to be added to reports.  Does anyone know the reports that use classes and characteristics or have the ability to use them.  Any help is appreciated.
    Regards,

    Consultant 01,
    I am not sure of the specifics of your question.  The reporting approach depends on the objective of the reports and the business requirements.  In general, transaction MC03 can be use to report via classification characteristics.  This key figure reporting option covers many classifying attributes.
    If you want to report using batch classification and characteristic values, BMBC offers options for reporting on batches.  In this case you may use the classification tab of BMBC to enter the batch class for "search with batch class" and/or "search with selection class".  The latter allows you to view and/or narrow your search by characteristic values.
    I hope that this helps.
    Regards,
    Wayne

  • When trying to move file and use command and drag, it doesn't move the file just creates a short cut.

    When trying to move file and use command and drag, it doesn't move the file just creates a short cut.

    Using command creates a copy.
    If you want to just move a file, click on it, hold it and drag it to the new location.

  • I recently tried to remove widows7 on my macbook air using bootcamp and halfway through it grayed out .The windows 7 is removed but i cant free up the 62gig partition the windows was occupying on my 121gig ssd harddrive please help

    i recently tried to remove widows7 on my macbook air using bootcamp and halfway through it grayed out .The windows 7 is removed but i cant free up the 62gig partition the windows was occupying on my 121gig ssd harddrive please help

    You may have to see about using the Recovery HD partition to attempt to erase and restore useful function of the capacity lost to the former Windows installation.
    And you may have to backup your contents outside the computer to do it. Disk Utility on the Mac OS X historically has not been able to touch a BootCamp created partition; so if BootCamp cannot remove it, then you have to see how to do the equivalent of a reformat of the entire drive (less Recovery partition?) then reinstall your OS X. Maybe from internet recovery, then get your applications, etc from the Mac App store, etc.
    A complete system clone of your OS X on an external HDD would be another method to restore after reformat, of the portion you want whole again, not necessarily the space where Recovery may live.
    There may be (an)other way(s).
    Perhaps someone with recent experience
    in this matter can reply & discuss options.
    With my antiques, they almost know what I want before I tell them.
    Hopefully yours will after you talk to it awhile.
    Good luck & happy computing!

  • "Setup encountered a problem while validating the state of Active Directory: Exchange organization-level objects have not been created, and setup cannot create them because the local computer is not in the same domain and site as the schema master. Run se

    Team,
    I am trying to Install Exchange on my Lab, getting below error
    message.
    The Schema Role is installed on Root Domain and trying to install
    exchange on Child domain.
    1 Root Domain - 1 Child domain. both are located on single site.
    “Setup encountered a problem while validating
    the state of Active Directory: Exchange organization-level objects have not been created, and setup cannot create them because the local computer is not in the same domain and site as the schema master. Run setup with the /prepareAD parameter and wait for
    replication to complete.”
    Followed below articles:
    http://support.risualblogs.com/blog/2012/02/21/exchange-2010-sp2-upgrade-issue-exchange-organization-level-objects-have-not-been-created-and-setup-cannot-create-them-because-the-local-computer-is-not-in-the-same-domain-and-site-as-the-sche/
    http://www.petenetlive.com/KB/Article/0000793.htm
    transferred the schema roles to different server on root domain, still no luck.
    can someone please help me.
    regards
    Srinivasa k
    Srinivasa K

    Hi Srinivasa,
    I guess, you didn't completed the initial setup schemaprep and adprep before starting the installation. You can do it as follows:
    1. Open command Prompt as administrator and browse to the root of installation cd and run Setup.exe /PrepareSchema /IAcceptExchangeServerLicenseTerms
    After finishing this,
    2. Setup.exe /PrepareAD /OrganizationName:"<organization name>" /IAcceptExchangeServerLicenseTerms
    3. To prepare all domains within the forest run Setup.exe /PrepareAllDomains /IAcceptExchangeServerLicenseTerms. If you want to prepare a specific domain run Setup.exe /PrepareDomain:<FQDN of the domain you want to prepare> /IAcceptExchangeServerLicenseTerms
    4. Once you complete all of the 3 steps, install the pre-requisities for Exchange 2013
    5. Finally, run the setup program
    Hope this will help you
    Regards from Visit ExchangeOnline |
    Visit WindowsAdmin

  • I was trying to sync a song from my computer to my iPhone 4 using iTunes and it said you can transfer the music from your phone to your itunes if you want to sync it so I did and none of my music is on my phone or on itunes besides the song i was syncing

    I was trying to sync a song from my computer to my iPhone 4 using iTunes and it said you can transfer the music from your phone to your itunes if you want to sync it so I did and none of my music is on my phone or on itunes besides the song i was syncing

    Hi Soccer4461,
    If you need to update your iTunes software, you may find the following article helpful:
    OS X: Updating OS X and Mac App Store apps
    http://support.apple.com/kb/ht1338
    Regards,
    - Brenden

  • I own acrobat 9 pro and had to re-download the program from a computer issue but when i get the serial code from the adobe website it give me serial code invalid error.  i am using copy and paste to prevent miss typing the code.

    i own acrobat 9 pro and had to re-download the program from a computer issue but when i get the serial code from the adobe website it give me serial code invalid error.  i am using copy and paste to prevent miss typing the code.

    Hi Lawrence,
    Please try the steps mentioned in the KB: https://helpx.adobe.com/creative-suite/kb/error-invalid-serial-number-acrobat.html
    Regards,
    Rave

  • While using my Iphone 5, all of a sudden  black and white stripes where on the screen. I have tryed to turn the phone off and on again, but there are only the stripes. I can't use it, and nothing happens when I push the buttons. Can someone help me?

    While using my Iphone 5, all of a sudden  black and white stripes where on the screen. I have tryed to turn the phone off and on again, but there are only the stripes. I can't use it, and nothing happens when I push the buttons. Can someone help me?

    Try resetting your phone, hold the home and sleep/wake button down until the apple symbol comes up, then release. If that doesnt work, you may have damaged your phone and need to take it to an apple genius bar.

  • I am trying to import movie files into to iMovie. They are home videos so I use handbrake and VLC to put them onto the computer and convert them to MPEG-4 files. I successfully imported two of the videos but the others are not working, they are all MPEG-4

    I am trying to import movie files into to iMovie. They are home videos so I use handbrake and VLC to put them onto the computer and convert them to MPEG-4 files. I successfully imported two of the videos but the others are not working, they are all MPEG-4 files. Now, they are not compatible. How to I get them into imovie?

    Hi
    I use
    • DV-PAL or DV-NTSC - or -
    • AIC
    Yours Bengt W

  • When moving a file using drag and drop, as I hover over the destination folder it no longer opens, so I can't drop the file into the folder.  Why?

    When moving a file using drag and drop, as I hover over the destination folder it no longer opens, so I can't drop the file into the folder.  Why?  I have a heirarchical embedded folder structure under \Documents.  Usually, as I drag the file to be moved over the various folders, they will blink twice, then expand/open to reveal contents (in Finder).  Now, when the file to be moved is held (hovering) over the destination folder, it will not blink or expand, so there's no place to drop the file.  If I release the mouse button, the file image disappears, showing that it is "going back to the place it came from", a.k.a. was not moved.  This just started happening.  I noticed it after upgrading to Mavericks.  Is this a bug or could anything else be causing this?

    As of today, Apple Support discovered during a remote support session with me that the same thing happens on their own system.  It is isolated to "Column View"; the Icon and List views in Finder behave correctly.  Issue is being escalated to engineering, and Apple will call to report status in a couple days.  In all likelihood, this is a bug that will need to be addressed in a future update.

  • Could someone tell me how to control a small dc motor by using labeled and a NI 6008 daq. The motor is adjustable by using a pot that ranges between 0-V if that is of any additional benefit

     Could someone tell me how to control a small dc motor by using labeled and a NI 6008 daq.  The motor is adjustable by using a pot  that ranges between 0-V if that is of any additional benefit.

    Read the second thread you have a link to. While both of these will produce PWM signals, the first one is a couple of orders of magnitude too slow to do anything useful in control the speed of a motor, what you would see would be the motor run at the full speed, then stop for a period, then run at full speed, rather than at the "average of on and off"  as the Pulse Widths will be very long relative to the motor's response. The second one has a better chance, IF all you want to do is run the motor. If there is any other calculations ...   Look at both and try and get an understanding of what they are doing. For PWM speed control of a motor you really need a pretty fast pulse width, and probably some filtering to smooth out the resulting "harsh" waveform, giving you the "average" voltage resulting from the PWM.
    Putnam
    Certified LabVIEW Developer
    Senior Test Engineer
    Currently using LV 6.1-LabVIEW 2012, RT8.5
    LabVIEW Champion

Maybe you are looking for

  • Combine checked item and update column

    Hi all, need to design a screen that allow user to update column (code) and also pick which statement to display for the next stage. My thought was to create a checkitem and LOV display. Once user has checked the box , the process should update the c

  • Main display removed - desktop

    With the main LCD display removed, will the OS automatically make an external display the default? or will it still try and display the tool bar on the non existent LCD?

  • Re: Help with Nomad Jukebox 3 and Creative Softw

    Okay I need help with the following...(bare with me!) First, I have a Nomad Jukebox 3. I record shows with it at concerts that allow me to record via the optical input. I currently use Creative Media Source but I don't have the pluggins to "Burn CD".

  • Printer requesting to load legal paper so print job fails

    The printer is a HP Laserjet 4 series PCL-5, device driver: HPLJ4. It has 3 trays: #1 is a bypass, #2 and #3 have 8.5x11 paper. This printer has been in use for some time, but suddenly the printer is now requesting to "load plain legal" when a print

  • AVG Security Toolbar states: Incompatible with Firefox 3.6.10

    AVG Security Toolbar (2.710.016.005) states: Incompatible with Firefox 3.6.10 worked in earlier Firefox releases other than 3.6.10