Hi all,
Can I execute programmaticlly the main of a class which I only the
..class file of?
|
|
0
|
|
|
|
Reply
|
meidan.alon
|
12/14/2005 5:47:55 PM |
|
"Meidan" <meidan.alon@gmail.com> wrote in message
news:1134582475.259137.205240@g44g2000cwa.googlegroups.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
|
|
0
|
|
|
|
Reply
|
no.offline.contact.please2 (560)
|
12/14/2005 6:25:21 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
not108 (1060)
|
12/14/2005 6:57:57 PM
|
|
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.
|
|
0
|
|
|
|
Reply
|
meidan.alon
|
12/14/2005 7:04:37 PM
|
|
Well, you'll need to make sure Foo is in the correct place in your
classpath when compiling
|
|
0
|
|
|
|
Reply
|
bcremers (87)
|
12/14/2005 7:10:44 PM
|
|
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.
|
|
0
|
|
|
|
Reply
|
meidan.alon
|
12/14/2005 7:15:14 PM
|
|
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.");
}
}
|
|
0
|
|
|
|
Reply
|
jeff34 (1594)
|
12/14/2005 7:21:05 PM
|
|
"Meidan" <meidan.alon@gmail.com> 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
|
|
0
|
|
|
|
Reply
|
zero15 (423)
|
12/14/2005 7:26:57 PM
|
|
"BartCr" <bcremers@gmail.com> 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
|
|
0
|
|
|
|
Reply
|
zero15 (423)
|
12/14/2005 7:27:44 PM
|
|
> 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();
}
|
|
0
|
|
|
|
Reply
|
thomas_okken (19)
|
12/14/2005 7:29:24 PM
|
|
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.......
|
|
0
|
|
|
|
Reply
|
meidan.alon
|
12/14/2005 8:07:17 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
not108 (1060)
|
12/14/2005 8:40:22 PM
|
|
On 14 Dec 2005 09:47:55 -0800, "Meidan" <meidan.alon@gmail.com> 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.
|
|
0
|
|
|
|
Reply
|
my_email_is_posted_on_my_website (4730)
|
12/14/2005 11:42:23 PM
|
|
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.
|
|
0
|
|
|
|
Reply
|
meidan.alon
|
12/15/2005 7:31:34 AM
|
|
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>
|
|
0
|
|
|
|
Reply
|
meidan.alon
|
12/15/2005 8:19:43 AM
|
|
|
14 Replies
20 Views
(page loaded in 0.244 seconds)
Similiar Articles: Calling Compiled Java Class - comp.lang.java.help... there is another method which I can use - I have the class ... the java class is compiled ... call the main method.. ... or its only .class that created by JCreator compiler ... class file has wrong version 49.0, should be 48.0 - comp.lang.java ...... The class file i generated using jdk 1.5 and I try > to execute it ... it on another machine, having only jre 1.4. From what I can guess of your post, you have two main ... call java class from Matlab - comp.soft-sys.matlab... want to call FROM Matlab the class that contains main ... posted by ImageAnalyst and another book ... class that created by JCreator compiler? how create object from my class ... How redirect event between two public class? - comp.lang.java.gui ...... about event from one object of one class to another (belongs to another class)? Main ... When I use GNU c++ version 4.3 to compile the code: #include #include class DATA ... Problem with pthreads C++ wrapper class on Linux - comp ...... id, pData->threadID); } }; int main(void ... object to wrap/define the thread's run/execute ... You could try this with your classes and it would compile, but it ... JAR file executables and ClassNotFound Exception - comp.lang.java ...... manifest-additions.mf file and add Main-Class ... and names of the manifest file and main class? 4) What command is used to execute ... that the servlet was originally compiled ... Another class of "DO10I=" bug - comp.lang.fortranThe nett effect is the same, but a compiler may think ... Yeah, that was the main/starting message of my last ... Another class of "DO10I=" bug - comp.lang.fortran [You may ... batch file/jar file - comp.lang.java.programmerBut basically it is; 1) Compile your program 2) Create a manifest file with at least this line; Main-Class ... java class using Runtime Class ... How to execute ... Undefined reference to public class method? - comp.lang.c++ ...For the following classes, just assume I have ... NumData object by adding a double and another ... Undefined reference to public class method? - comp.lang.c++ ... int main ... Call a test from within a test junit - comp.lang.java.help ...... setup () { //Here I want to class another test class ... How can I call Setup and execute methods from my class A? ... testing of multiple test cases. ... of main ... Application error occurred during request processing - comp.lang ...I dont get any exceptions while i compile, but> i get this error when i am executing.>> i have two classes, which I ... in order > to startup successfully, or another ... ELFCLASS32 KILLED - comp.unix.solarisHi all, when i execute this particulare code I get this error: ld.so.1: ./int:fatal:/usr/lib/64/libstdc++.so.6:wrong ELF class ... solution is to insure you compile ... trying to figure things out about chars - comp.lang.java.help ...I have a very simple program which won't compile. /** * */ package data; import java.lang.Character; public class MyStringsAndChars { public static void main ... Is "stack-based" object design for ref class really a good design ...... The big problem is that , by default, C++/CLI compiler ... to provide the stack-based objects for ref class ? Another ... The main implementation will always be to put it on ... How to check if object is an instanceof a class, but not a sub ...My main point would be that a real-world taxonomy is ... Jdk 1.6 will not compile: cannot access java.lang ... Another class of "DO10I=" bug - comp.lang.fortran Orphaned ... How to execute a java .class from the command line - Stack OverflowFirst, have you compiled the class using the ... the command line arguments from another class ... Java code compiles but does not execute : “Could not find the main class” Compiling, Executing, and Jar'ing Java Code - USFCSCompile as before (except you are in the foo) subdirectory: ... Find file T.class inside directory foo. Execute method main() within Jar'ing Java Code 7/2/2012 3:20:13 AM
|