Executing a compiled class' main from another class

Discussion in 'Java' started by Meidan, Dec 14, 2005.

  1. Meidan

    Meidan Guest

    Hi all,

    Can I execute programmaticlly the main of a class which I only the
    ..class file of?
     
    Meidan, Dec 14, 2005
    #1
    1. Advertising

  2. Meidan

    Rhino Guest

    "Meidan" <> wrote in message
    news:...
    > Hi all,
    >
    > Can I execute programmaticlly the main of a class which I only the
    > .class file of?
    >

    You can but do you really want to?

    If you want to execute a file called Foo.class directly from the command
    line, you automatically execute main() first (assuming Foo is an
    application, not an applet). For example:

    java Foo

    will always execute the main() method first.

    If you invoke Foo from a class called Bar, it will look like this:

    =======================
    public class Bar {

    Foo myFoo = new Foo();
    }
    ========================

    but the method called will be the appropriate Foo constructor (the one that
    takes no parameters in this example) and not main().

    It would be rather unusual to want to invoke the main() method of Foo from
    Bar. Invoking some other method of Foo wouldn't be that unusual but main()
    is typically only invoked by running Foo from the command line.

    However, you could probably invoke main() via reflection if you really
    wanted to. I believe it is somewhat expensive but it should be possible. I
    don't have a link to a tutorial on Reflection handy but you should be able
    to find one via Google if you really want to execute Foo's main from another
    class.

    Rhino
     
    Rhino, Dec 14, 2005
    #2
    1. Advertising

  3. On Wed, 14 Dec 2005 13:25:21 -0500, Rhino wrote:
    > If you invoke Foo from a class called Bar, it will look like this:


    Or even simpler:

    Foo.main(args);

    No need for an instance, constructors or reflection.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Dec 14, 2005
    #3
  4. Meidan

    Meidan Guest

    Re: Executing a compiled class' main from another class

    Gordon Beaton wrote:
    > On Wed, 14 Dec 2005 13:25:21 -0500, Rhino wrote:
    > > If you invoke Foo from a class called Bar, it will look like this:

    >
    > Or even simpler:
    >
    > Foo.main(args);
    >
    > No need for an instance, constructors or reflection.
    >
    > /gordon
    >
    > --
    > [ do not email me copies of your followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e


    I already tried that.
    But it wont compile - it says Foo cannot be resolved.
     
    Meidan, Dec 14, 2005
    #4
  5. Meidan

    BartCr Guest

    Re: Executing a compiled class' main from another class

    Well, you'll need to make sure Foo is in the correct place in your
    classpath when compiling
     
    BartCr, Dec 14, 2005
    #5
  6. Meidan

    Meidan Guest

    Re: Executing a compiled class' main from another class

    BartCr wrote:
    > Well, you'll need to make sure Foo is in the correct place in your
    > classpath when compiling


    But I didn't compile Foo, I only have the .class.
     
    Meidan, Dec 14, 2005
    #6
  7. Meidan wrote:
    > Hi all,
    >
    > Can I execute programmaticlly the main of a class which I only the
    > ..class file of?


    // Yes.

    import java.io.PrintWriter;
    import java.lang.reflect.Method;
    import java.lang.reflect.InvocationTargetException;

    public class ClassMain {

    static PrintWriter err = new PrintWriter(System.err, true);

    public static void main(String[] args) {
    callMain(Helper.class, args);
    }

    static void callMain(Class c, Object args) {
    try {
    Method m = c.getDeclaredMethod(
    "main", String[].class);

    m.invoke(null, args);

    } catch(NoSuchMethodException x) {
    err.println(x);
    } catch(IllegalAccessException x) {
    err.println(x);
    } catch(InvocationTargetException x) {
    err.println(x);
    }
    }
    }

    class Helper {

    static PrintWriter out = new PrintWriter(System.out, true);

    public static void main(String[] args) {
    out.println("Hello from Helper.");
    }
    }
     
    Jeffrey Schwab, Dec 14, 2005
    #7
  8. Meidan

    zero Guest

    Re: Executing a compiled class' main from another class

    "Meidan" <> wrote in news:1134587714.717782.182840
    @g14g2000cwa.googlegroups.com:

    >
    > BartCr wrote:
    >> Well, you'll need to make sure Foo is in the correct place in your
    >> classpath when compiling

    >
    > But I didn't compile Foo, I only have the .class.
    >


    Well, you'll need to make sure Foo is in the correct place in your
    classpath when compiling Bar.

    ie: javac -classpath path/to/Foo/ Bar.java

    --
    Beware the False Authority Syndrome
     
    zero, Dec 14, 2005
    #8
  9. Meidan

    zero Guest

    Re: Executing a compiled class' main from another class

    "BartCr" <> wrote in news:1134587444.240877.166390
    @g14g2000cwa.googlegroups.com:

    > Well, you'll need to make sure Foo is in the correct place in your
    > classpath when compiling
    >


    Also make sure you import it if it's defined in a package.

    --
    Beware the False Authority Syndrome
     
    zero, Dec 14, 2005
    #9
  10. Meidan

    Thomas Okken Guest

    Re: Executing a compiled class' main from another class

    > But I didn't compile Foo, I only have the .class.

    That's not a problem, all you need is the .class. But, it needs to be
    in your classpath, so that the compiler can find it; the compiler needs
    to see it so that it can check your code (e.g. to see whether or not
    Foo contains a main() method, or else it won't be able to generate code
    for the Foo.main() call).

    Alternatively, use the reflection approach mentioned earlier:

    try {
    Class c = Class.forName("Foo");
    String[] args = new String[] { "Hello", "world" };
    Method m = c.getMethod("main", new Class[] { args.getClass() });
    m.invoke(null, args);
    } catch (Exception e) {
    // Possible exceptions include ClassNotFoundException,
    // NoSuchMethodException, InvocationTargetException
    e.printStackTrace();
    }
     
    Thomas Okken, Dec 14, 2005
    #10
  11. Meidan

    Meidan Guest

    Re: Executing a compiled class' main from another class

    Thomas Okken wrote:
    > > But I didn't compile Foo, I only have the .class.

    >
    > That's not a problem, all you need is the .class. But, it needs to be
    > in your classpath, so that the compiler can find it; the compiler needs
    > to see it so that it can check your code (e.g. to see whether or not
    > Foo contains a main() method, or else it won't be able to generate code
    > for the Foo.main() call).
    >
    > Alternatively, use the reflection approach mentioned earlier:
    >
    > try {
    > Class c = Class.forName("Foo");
    > String[] args = new String[] { "Hello", "world" };
    > Method m = c.getMethod("main", new Class[] { args.getClass() });
    > m.invoke(null, args);
    > } catch (Exception e) {
    > // Possible exceptions include ClassNotFoundException,
    > // NoSuchMethodException, InvocationTargetException
    > e.printStackTrace();
    > }


    I get a java.lang.ClassNotFoundException when Class.forName("Foo") is
    executed.

    I'm using Eclipse and I found in project properties the parameter "java
    build path", I added to it the location of my .class file. But still no
    luck.......
     
    Meidan, Dec 14, 2005
    #11
  12. Re: Executing a compiled class' main from another class

    On 14 Dec 2005 12:07:17 -0800, Meidan wrote:
    > I get a java.lang.ClassNotFoundException when Class.forName("Foo")
    > is executed.


    So you've got classpath issues. Does "Foo" have a real name? Does it
    belong to a package?

    If so, the classpath should not point to the directory containing the
    classfile. It should point to a directory containing a hierarchy of
    directories that correspond to the component parts of the package name.

    Learn more about the necessary relationship between the classpath and
    the directory structure here:

    http://www.yoda.arachsys.com/java/packages.html

    Type "javap Foo" to see what the file contains if you don't already
    know.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Dec 14, 2005
    #12
  13. Meidan

    Roedy Green Guest

    On 14 Dec 2005 09:47:55 -0800, "Meidan" <> wrote,
    quoted or indirectly quoted someone who said :

    >Can I execute programmaticlly the main of a class which I only the
    >.class file of?


    of course. Most of the time you don't get source from the authors.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 14, 2005
    #13
  14. Meidan

    Meidan Guest

    Re: Executing a compiled class' main from another class

    Gordon Beaton wrote:
    > On 14 Dec 2005 12:07:17 -0800, Meidan wrote:
    > > I get a java.lang.ClassNotFoundException when Class.forName("Foo")
    > > is executed.

    >
    > So you've got classpath issues. Does "Foo" have a real name? Does it
    > belong to a package?
    >
    > If so, the classpath should not point to the directory containing the
    > classfile. It should point to a directory containing a hierarchy of
    > directories that correspond to the component parts of the package name.
    >
    > Learn more about the necessary relationship between the classpath and
    > the directory structure here:
    >
    > http://www.yoda.arachsys.com/java/packages.html
    >
    > Type "javap Foo" to see what the file contains if you don't already
    > know.
    >
    > /gordon
    >
    > --
    > [ do not email me copies of your followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e


    My file system structure is:
    Exercise
    FruitGamePlayer
    .metadata(dir)
    Player
    .metadata
    .classpath
    .project
    Tester.java
    MyPlayer.java
    (and some more .class files for internal use in MyPlayer)

    FruitGamesServer
    .metadata(dir)
    GamesServer
    .project
    GameDefinition.class
    GameServerMain.class
    HandlingServers.class
    PlayerServer.class
    .classpath
    .project

    What do I have to do in order to call GameServerMain.main() from
    Tester.java?
    Note that I can call it from the command line by java -classpath .;..
    GameServerMain from within the GameServer Directory.
     
    Meidan, Dec 15, 2005
    #14
  15. Meidan

    Meidan Guest

    Re: Executing a compiled class' main from another class

    The contents of my main .classpath file:
    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
    <classpathentry kind="src" path="FruitGamePlayer/Player"/>
    <classpathentry kind="src" path="FruitGameServer/GameServer"/>
    <classpathentry kind="con"
    path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="con" path="FruitGameServer/GameServer"/>
    <classpathentry kind="con" path="FruitGameServer/GameServer"/>
    <classpathentry kind="output" path="FruitGamePlayer/Player"/>
    <classpathentry kind="output" path="FruitGameServer/GameServer"/>
    </classpath>
     
    Meidan, Dec 15, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cyril
    Replies:
    2
    Views:
    3,869
    cyril
    Aug 25, 2004
  2. Klaus Schneider
    Replies:
    1
    Views:
    551
    Rolf Magnus
    Dec 2, 2004
  3. O.B.
    Replies:
    0
    Views:
    477
  4. Grzesiek
    Replies:
    5
    Views:
    12,885
  5. lander
    Replies:
    5
    Views:
    597
    bruce barker
    Mar 5, 2008
Loading...

Share This Page