views:

111

answers:

3

Is there a way to reconstruct the command line arguments passed to Java within a Java program, including the JVM options and classpath option?

I have a Java program that needs to restart the JVM and manipulate its bootclasspath (i.e. trying to override some system classes). I use the libc system method to invoke the new JVM.

I'm open for better approaches, but Java agents isn't an option.

A: 

Err... modifying a whole core java class at runtime is a very very bad idea.

Whats wrong with subclassing here? Are you trying to modify an external library, add functionality, or be lazy?

TheLQ
Why the downvote?
TheLQ
+1  A: 

Why not use a file that has these properties just like the Eclipse ini file and NetBeans conf files. That way you just read these properties and spawn the new Java process with these properties.

Back to your question, this previous answer should do

n002213f
Thanks! `RuntimeMZBean.getInputArguments()` solves the problem quite well.
notnoop
A: 

I agree that futzing with the bootclasspath is generally a poor idea. But...

Grab the code for "java.c" - the C program that compiles down to java.exe. You'll find that it just uses the JNI Invocation API to construct a JVM and call the main method. You could modify and re-compile this to look for particular exit codes, etc. and loop around and re-launch the JVM if required.

Alternatively, Eclipse does this (or at least used to), but having one Java program construct the command line (from a props file, etc.) and launch a sub-process. Again, it hooked the sub-process exit code and used that to decide whether or not to re-launch a new sub-process.

dty