Results 1 to 8 of 8
  1. #1
    ketan is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default run a .class file at run time from another java file

    Hello All,

    I want to run a .class file at run time from another java file ,,, plz help me how can i code for this,,,,
    moreover if that .class file doesnt contain "main", is it feasible to run using "main" of this caller java file....


    Thanks in advance!

    Ketan

  2. #2
    dinesh.guntha is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default Re: run a .class file at run time from another java file

    Runtime.getRuntime().exec(cmd c:\java file.java);
    but it should contain the main method then only it will run

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: run a .class file at run time from another java file

    I want to run a .class file at run time from another java file
    Call the class's constructor if it has a public one:
    Java Code:
    TheClassName = new TheClassName(????); // call the class's constructor
    Otherwise you need to know the name of a static method in the class that you can call like the main class:
    Java Code:
    TheClassName.main(new String[]{}); // call the class's main method

  4. #4
    ketan is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: run a .class file at run time from another java file

    Hello Dinesh, Norm,
    Thanks for replying. Actually I dont have source code(java file) of that file in my system , I have only class file that i need to run by writing code for it.... please suggest

  5. #5
    dinesh.guntha is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default Re: run a .class file at run time from another java file

    Hello Ketan,

    Create a java class with main then call that class in this main class it will work i think so,

    For example Dog.class

    Class Anaimal {
    public static void main(String args[]){
    Dog g = new Dog();
    g // u can access the all the other than private access specifier
    }
    } or
    By using the reflection you can access class

    you can try with the reflection i think so

  6. #6
    ketan is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: run a .class file at run time from another java file

    Thanks Dinesh! There is some issue in this ,,,,,,,
    This code will look for Dog class at compile time, but Dog.class file wont be available before compilation,,,,

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: run a .class file at run time from another java file

    Put the Dog class file on the classpath during compilation.

  8. #8
    ketan is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: run a .class file at run time from another java file

    hey , i was able to do it using reflection.... Thanks a lot Dinesh and Norm :)

Similar Threads

  1. File copying and time stamp extraction(java)
    By yande in forum AWT / Swing
    Replies: 3
    Last Post: 05-31-2011, 06:39 PM
  2. Add a jar file to Java load path at run time
    By marioneil in forum Advanced Java
    Replies: 5
    Last Post: 09-22-2010, 07:58 AM
  3. need help.. java class file...
    By Madz in forum New To Java
    Replies: 3
    Last Post: 11-21-2009, 03:42 AM
  4. converting java class file to exe file
    By satheeshtech in forum Advanced Java
    Replies: 5
    Last Post: 07-18-2009, 11:47 PM
  5. .class file to .java file
    By makpandian in forum New To Java
    Replies: 1
    Last Post: 02-20-2009, 06:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •