WebLogic Server — using WLST commands from Java


bea_logoI have been working with WebLogic Server since 1999 when it was still a BEA Systems product. Though professionally I would characterize myself as an architect/developer, I have been working with WebLogic administrators for more than a decade. In fact, at BEA Systems I was the author of the original BEA WebLogic Administrator Certification exam. As a WebLogic consultant, the primary tools that I use with clients are the WebLogic Scripting Tool (WLST) and the WebLogic Diagnostic Framework (WLDF).

WLST is based on Jython, the all-Java version of Python. Since Jython runs in the JVM it is possible to invoke WLST commands directly form Java code. The possibilities are endless, you could use a Swing Gui front end and a WLST back end to provide automated management and monitoring for WebLogic.

Here is a simple example of WLST commands invoked from Java code. This code example uses a Runtime MBean to check how long the server has been running.

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.IOException;
import java.io.OutputStream;

// from weblogic.jar
import org.python.core.PyLong;
import org.python.util.InteractiveInterpreter;
import weblogic.management.scripting.utils.WLSTInterpreter;

public class WSTLClient
{
  public static void main(String[] args)
  {
    InteractiveInterpreter interpreter = new WLSTInterpreter();    

    // WLST output to dev/null
    interpreter.setOut(new NullOutputStream());

    // Run commands no results
    interpreter.exec("connect('user','password','t3://host:port')");
    interpreter.exec("serverRuntime()");

    // Run command with result
    PyLong result = (PyLong)interpreter.eval("cmo.getActivationTime()");

    // calculate uptime
    long startTime = result.getValue().longValue();    
    long currentTime = System.currentTimeMillis();
    long uptimeMilli = currentTime - startTime;
    long uptimeMins = (uptimeMilli / 1000L) / 60L;

    System.out.println( "Server Uptime: " + uptimeMins + "min");
  }

  // custom class to iplement noop output stream
  public static class NullOutputStream extends OutputStream
  {
    @Override public void write(int b) throws IOException {}
    @Override public void write(byte[] b) throws IOException {}
    @Override public void write(byte[] b, int off, int len) throws IOException { }
  }
}

WLST documentation can be found here and here. Feel free to use this code as the basis for your own scripts.

For more information on WebLogic Administration, Web Age Solutions offers a full line of WebLogic training courses. Check out our WebLogic classes at WebAgeSolutions.com.

  1. #1 by Divya on July 31, 2013 - 7:29 am

    Hi,

    I tried to run the code above as Java application.But getting the below exception :
    Exception in thread “main” java.lang.NullPointerException
    at java.io.File.(Unknown Source)
    at weblogic.management.utils.PDevHelper.getUpgradeLaunchLocation(PDevHelper.java:77)

    Can you please help me with it and provide me any configuration changes are required.I have added all the required jars in classpath.

  2. #2 by Stuart Smith on September 11, 2013 - 10:01 pm

    Without seeing the exact code you have it is hard to say.

    I would say the most likely issue is in the lines that use ‘interpreter.exec’. These lines have double and single quotes, forward slashes (to avoid the ‘escape the backslash in Java’ issue), and you have to substitute your user, password, and host:port of the WebLogic server.

  3. #3 by bibin on December 24, 2014 - 7:25 am

    I am hitting with the same issue divya has mentioned.

    Exception in thread “main” java.lang.NullPointerException
    at java.io.File.(File.java:251)
    at weblogic.management.utils.PDevHelper.getUpgradeLaunchLocation(PDevHelper.java:77)
    at weblogic.management.utils.PDevHelper.getPDevClassLoader(PDevHelper.java:36)
    at weblogic.management.scripting.utils.WLSTUtil.setupOffline(WLSTUtil.java:226)
    at weblogic.management.scripting.utils.WLSTInterpreter.(WLSTInterpreter.java:134)
    at weblogic.management.scripting.utils.WLSTInterpreter.(WLSTInterpreter.java:76)
    at HelloClient.main(HelloClient.java:6)

    Code is

    package weblogic;

    import org.python.core.PyLong;
    import org.python.util.InteractiveInterpreter;
    import weblogic.management.scripting.utils.WLSTInterpreter;
    public class HelloClient {
    public static void main(String[] args) {
    InteractiveInterpreter interpreter = new WLSTInterpreter();
    interpreter.exec(“connect(‘weblogic’,’weblogic1′,’t3://myserver:7011′)”);
    interpreter.exec(“serverRuntime()”);
    PyLong result = (PyLong) interpreter.eval(“cmo.getActivationTime()”);
    long startTime = result.getValue().longValue();

    System.out.println(“Server Uptime: ” + startTime + “min”);
    }

    }

  4. #4 by Abe on April 16, 2015 - 8:04 pm

    Hi,
    I try to running the code but it was failed, with the following error :

    Exception in thread “main” java.lang.NoClassDefFoundError: weblogic/management/scripting/utils/WLSTMsgTextFormatter
    at weblogic.management.scripting.utils.WLSTUtilHelper.(WLSTUtilHelper.java:36)
    at weblogic.management.scripting.utils.WLSTInterpreter.(WLSTInterpreter.java:104)
    at wlst.WSTLClient.main(WSTLClient.java:13)
    Caused by: java.lang.ClassNotFoundException: weblogic.management.scripting.utils.WLSTMsgTextFormatter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    … 3 more
    Java Result: 1

    Did I missing something? I’ve tried to search using googling about where I can find/download those class but no result.

    Any help is appreciated.

    Thx.

  5. #5 by Abe on April 16, 2015 - 8:07 pm

    -continuing previous comment-

    the environment configuration that i used are :
    Netbeans 8 IDE
    weblogic-classes.jar (from WLAS 12c)
    JDK 8
    OS Windows 7

    Can anyone give hint about my issue?
    Any help is appreciated.
    Thx.

  6. #6 by swatantra on September 5, 2015 - 5:35 pm

    same problem. tried enough of googling but couldn’t find this class. Please help

    Exception in thread “main” java.lang.NoClassDefFoundError: weblogic/management/scripting/utils/WLSTMsgTextFormatter
    at weblogic.management.scripting.utils.WLSTUtilHelper.(WLSTUtilHelper.java:36)
    at weblogic.management.scripting.utils.WLSTInterpreter.(WLSTInterpreter.java:104)
    at wlst.WSTLClient.main(WSTLClient.java:13)
    Caused by: java.lang.ClassNotFoundException: weblogic.management.scripting.utils.WLSTMsgTextFormatter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) – See more at: http://blog.webagesolutions.com/archives/879#sthash.rVY8tq0J.dpuf

  7. #7 by Chellappan S on September 24, 2015 - 11:25 am

    I am excuting basic WLST command, when I does, I am getting below error:

    java.lang.NullPointerException
    at weblogic.management.scripting.utils.WLSTInterpreter.(WLSTInterpreter.java:116)
    at weblogic.management.scripting.utils.WLSTInterpreter.(WLSTInterpreter.java:76)
    at WLSTClient.main(WLSTClient.java:12)

(will not be published)

*