HOWTO get the "Main-Class-Name" ?

A

Andreas Otto

Hi,

I want to write a java library able to spawn the Main class again.
For this feature I need the java executable and the "Main" class or
the jar file.

The executable is no problem because I get java.home from the properties.
The problem is the Main-Class-Name defined in "man java":

.... By default,the first non-option argument is the name of the class to be
invoked. ....

Question: HOWTO get the "Main-Class-Name" ?


mfg

Andreas Otto
 
J

John B. Matthews

Andreas Otto said:
I want to write a java library able to spawn the Main class again.
For this feature I need the java executable and the "Main" class or
the jar file.

The executable is no problem because I get java.home from the
properties. The problem is the Main-Class-Name defined in "man java":

... By default, the first non-option argument is the name of the
class to be invoked. ....

Question: HOWTO get the "Main-Class-Name" ?

I am unable to find the symbol "Main-Class-Name" in "man java":

<http://java.sun.com/javase/6/docs/technotes/tools/solaris/java.html>
<http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html>

You can store the Main-Class name in a manifest file as part of a Java
ARchive (JAR):

<http://java.sun.com/javase/6/docs/technotes/guides/jar/index.html>
<http://java.sun.com/docs/books/tutorial/jar/>

The question of launching a new JVM from within an existing one was
discussed recently:

<http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thre
ad/5ec78f64023dc272/99631a5e9b7bdd60>
 
R

Robert Kochem

Andreas said:
I want to write a java library able to spawn the Main class again.
For this feature I need the java executable and the "Main" class or
the jar file.

The executable is no problem because I get java.home from the properties.
The problem is the Main-Class-Name defined in "man java":

... By default,the first non-option argument is the name of the class to be
invoked. ....

Or it is defined by the MANIFEST in a jar file.

I would try to get the Stacktrace from the main thread. I haven't tried it
but I assume the deepest entry should indicate the main() method of the
Main-Class.

Robert
 
L

Lew

Robert said:
Or it is defined by the MANIFEST in a jar file.

I would try to get the Stacktrace from the main thread. I haven't tried it
but I assume the deepest entry should indicate the main() method of the
Main-Class.

That assumes the original main thread is still active, which it isn't always,
e.g., in Swing apps.

public Main
{
private Main(){}
public static void main( String [] args )
{
SwingUtilities.invokeLater( new Runnable()
{
@Override public void run()
{
new MySwingApp().run();
}
});
}
}

What good would knowing the main class do in this scenario?
Won't any stack trace you could obtain from 'MySwingApp' omit the "main"
thread? I don't know until I check or one of you all answer, but I guess that
'main()' is no longer on the stack once it's finished launching the Swing app.
 
A

Andreas Otto

Hi,

an example helps:

Main.javapackage MqS;

public abstract class Main {

static {
System.loadLibrary("javamsgque");
1. java
2. Other
}

....

}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Server.javapublic class Server extends MqS.Main implements MqS.IServer {
public Server(String[] argv) {
super(argv);
}

....
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


Other.javapublic class Other {
public static void main(String[] argv) {
Server sv = new Server(argv);
...
}
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

commandline:
java Other


Thanks for your help ....
 
M

Mark Space

Andreas said:
Hi,

an example helps:

Main.java
package MqS;

public abstract class Main {

static {
System.loadLibrary("javamsgque");
1. java
2. Other
}

...

}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Server.java
public class Server extends MqS.Main implements MqS.IServer {
public Server(String[] argv) {
super(argv);
}

...
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


Other.java
public class Other {
public static void main(String[] argv) {
Server sv = new Server(argv);
...
}
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

commandline:
java Other


Thanks for your help ....
 
M

Mark Space

Your example is really confusing to me. I have no idea what you are
actually trying to do. In general, if you want to load a class but
don't know it at compile time, you might use:

String name = ...
Class<IServer>getClass().forName( name );

The string "name" is set by the user, maybe via the command line, or as
part of a local config file. Reading the command line or a file is a
trivial exercise that I'll leave to you.

If this doesn't help, please try to provide a correctly compiling
example. BTW, Java naming conventions don't put an I in front of
interface names... just Server is preferable.
 
L

Lew

Mark said:
Your example is really confusing to me. I have no idea what you are
actually trying to do.

Same here.
BTW, Java naming conventions don't put an I in front of
interface names... just Server is preferable.

And don't use long strings of ">" characters in Usenet posts.

That is the Usenet convention for quoted posts, and some newsreaders
substitute fancy colored lines for these ">" characters. With dozens of them,
it is very distracting.

Even with plain-text newsreaders, long lines of ">" don't add any value.
 
M

Mark Space

Mark said:
String name = ...
Class<IServer>getClass().forName( name );


Oops, this got loused up during editing.

The basic syntax is

getClass().forName( name );

but you normally have to assign it to something, and cast it too

Class<IServer> serverClass = (Class<IServer>) getClass().forName( ...
 
L

Lew

Mark said:
Oops, this got loused up during editing.

The basic syntax is

getClass().forName( name );

but you normally have to assign it to something, and cast it too

Class<IServer> serverClass = (Class<IServer>) getClass().forName( ...

Which, of course, runs into a warning about an unchecked cast.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top