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------------------");
}
}



About Me