Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Executing a compiled class' main from another class

Reply
Thread Tools

Executing a compiled class' main from another class

 
 
Meidan
Guest
Posts: n/a
 
      12-14-2005
Hi all,

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

 
Reply With Quote
 
 
 
 
Rhino
Guest
Posts: n/a
 
      12-14-2005

"Meidan" <> wrote in message
news: oups.com...
> 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




 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      12-14-2005
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
 
Reply With Quote
 
Meidan
Guest
Posts: n/a
 
      12-14-2005

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.

 
Reply With Quote
 
BartCr
Guest
Posts: n/a
 
      12-14-2005
Well, you'll need to make sure Foo is in the correct place in your
classpath when compiling

 
Reply With Quote
 
Meidan
Guest
Posts: n/a
 
      12-14-2005

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.

 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      12-14-2005
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.");
}
}
 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      12-14-2005
"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
 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      12-14-2005
"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
 
Reply With Quote
 
Thomas Okken
Guest
Posts: n/a
 
      12-14-2005
> 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();
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If I create a page, then it's compiled upon first request, where cani find the compiled code?? lander ASP .Net 5 03-05-2008 04:34 PM
Could not find Main Class while executing JAR Grzesiek Java 5 09-10-2007 12:12 AM
g++ compiled C++ code called from gcc compiled C code Klaus Schneider C++ 1 12-02-2004 01:44 PM
instanciate a class in a jar file with class.forname, while my main class is in another jar cyril Java 2 08-25-2004 06:55 AM



Advertisments