B4J Question [SOLVED] Is there a way to read VM args passed to a jar from within the jar

JackKirk

Well-Known Member
Licensed User
Longtime User
I have a need to be able to log the VM args passed to a jar from within the running jar.

Is there any way to do this?

Thanks in anticipation...
 

Roycefer

Well-Known Member
Licensed User
Longtime User
B4X:
Sub VmArgs As List
    Dim jo As JavaObject = Me
    Return jo.RunMethod("getVmArgs",Null)
End Sub

#IF JAVA
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

public static java.util.List getVmArgs()
{
    RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
    anywheresoftware.b4a.objects.collections.List res = new anywheresoftware.b4a.objects.collections.List();
    res.Initialize();
    for(String s : runtimeMxBean.getInputArguments())
    {
        res.Add(s);
    }
    return res.getObject();
}
#END IF
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Works beautifully, many thanks...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Same implementation without inline code:
B4X:
Sub VmArgs As List
   Dim ManagementFactory As JavaObject
   ManagementFactory.InitializeStatic("java.lang.management.ManagementFactory")
   Dim runtimeMxBean As JavaObject = ManagementFactory.RunMethod("getRuntimeMXBean", Null)
   Return runtimeMxBean.RunMethod("getInputArguments", Null)
End Sub
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Just updated to Erel's version - also works beautifully.

I continue to be in awe of the support provided by all participants in these forums.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Just updated to Erel's version - also works beautifully.

I may have been a bit too quick - I'm noticing the following rather nasty sounding warnings in the log when I run Erel's version in debug mode:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by anywheresoftware.b4j.object.JavaObject (file:/C:/Program%20Files%20(x86)/Anywhere%20Software/B4J/Libraries/JavaObject.jar) to method sun.management.RuntimeImpl.getInputArguments()
WARNING: Please consider reporting this to the maintainers of anywheresoftware.b4j.object.JavaObject
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
 
Upvote 0

Similar Threads

Top