Tuesday, September 14, 2010

Running JBOSS Programmatically from java

Please find below code to run JBOSS Programmatically from JAVA. Please include jboss jars in your classpath.

I tested the following code for JBOSS-6.0.0.M2


package com.sudipta.jboss.conf;

import org.jboss.Main;

public class JbossOperation {
public static Main ob;
static Class jbossMain;
static{
try {
jbossMain=Class.forName("org.jboss.Main");
ob = (Main)jbossMain.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

}

/**
* START JBOSS SERVER
* @return true if started successfully
*/
public static boolean startServer(){
boolean status=true;

String str[]={"-c","default"};
try {
ob.boot(str);

} catch (Exception e) {
e.printStackTrace();
return false;
}
return status;
}

/**
* STOP JBOSS SERVER
* @return true if started successfully
*/
public static boolean stopServer(){
boolean status=true;
try {
ob.shutdown();
} catch (Throwable e) {
e.printStackTrace();
}

return status;
}

/**
* Main method
* @param args
*/
public static void main(String args[]){
System.out.println("---------------------Strating the server------------------");
startServer();
System.out.println("---------------------Stoping the server------------------");
stopServer();
System.out.println("---------------------SERVER STOPPED------------------");
}
}



5 comments:

  1. i have written the code but it is not working please help
    i have written it in zul file in eclipse it is zk file


    import org.jboss.Main;
    public boolean startJboss()
    { boolean stat=true;
    try
    {
    String[] str={"-c","default"};
    Class c = Class.forName("org.jboss.Main");
    Main ob=(Main)c.newInstance();
    ob.main(str);

    }
    catch(Exception e)
    {
    System.out.print(""+e.getMessage());
    stat=false;
    }
    return stat;
    }

    ReplyDelete
  2. Can you please mention the error or exception you are getting.

    ob.main(str);here try ob.boot(str);

    ReplyDelete
  3. How can you do it remotely? Where can I specify which host do I want to start/stop?

    ReplyDelete
  4. Hi. Thanks for this. It's just what I need, excep that I get the following exceptin on startup. We're running JBoss 6. Any ideas would be greatly appreciated. Thanks.


    19:38:30,796 ERROR [AbstractKernelController] Error installing to Configured: name=ServiceMetaDataICF state=Instantiated: java.lang.RuntimeException: Error configuring property: controller for ServiceMetaDataICF
    at org.jboss.kernel.plugins.dependency.ConfigureAction.dispatchSetProperty(ConfigureAction.java:112) [jboss-kernel.jar:2.2.0.Alpha10]

    ReplyDelete
  5. I know it has been a while since you posted this, but I tried to use this code using JBOSS-6.0.0.M2 and it didn't work. I get the following error:

    16:31:44,412 ERROR [AbstractKernelController] Error installing to Configured: name=ServiceMetaDataICF state=Instantiated: java.lang.RuntimeException: Error configuring property: controller for ServiceMetaDataICF
    at org.jboss.kernel.plugins.dependency.ConfigureAction.dispatchSetProperty(ConfigureAction.java:112)

    ...

    Caused by: java.lang.IllegalArgumentException: No such property controller for bean org.jboss.system.deployers.managed.ServiceMetaDataICF available [type, class, mbeanServer]
    at org.jboss.beans.info.plugins.AbstractBeanInfo.getProperty(AbstractBeanInfo.java:147)

    That I believe is the same error Calvin had. I was wondering if you did something else to make it work.

    Thank you very much

    ReplyDelete

About Me