This discussion is archived
5 Replies Latest reply: Dec 20, 2012 4:12 AM by Assaf.Lowenstein RSS

How to call a WLST from JAVA

890977 Newbie
Currently Being Moderated
Hello,
I have WLST script, which will connect to a running admin server and print the server status.

How to call the below script from JAVA
import sys
#@PydevCodeAnalysisIgnore
username = 'weblogic'
password = '1qaz2wsx'
URL='t3://localhost:7001'

connect(username,password,URL)
domainConfig()
serverList=cmo.getServers();
print ' Total Number of Servers ==>' +' ' +`len(serverList)`+ ''


domainRuntime()
cd('/ServerLifeCycleRuntimes/')

for server in serverList:
     name=server.getName()
   
   
     cd(name)
   
     serverState=cmo.getState()
     if serverState=='RUNNING':
        print '['
         
        print 'Server Name::'+' '+ name+'  '
        print 'Server State::'+' '+serverState+'  '
        print 'Server Port::'+' '+str(server.getListenPort())+'  '
        print ']'
       
     elif serverState=='SHUTDOWN':
        print '['
        print 'Server Name::'+' '+ name+'  '
        print 'Server State::'+' '+serverState+'  '
        print 'Server Port::'+' '+str(server.getListenPort())+'  '
        print ']'
     
     elif serverState=='UNKNOWN':
        print '['
        print 'Server Name::'+' '+ name+'  '
        print 'Server State::'+' '+serverState+'  '
        print 'Server Port::'+' '+str(server.getListenPort())+'  '
        print ']'
         
     
     cd('..')
Thanks,
Alok

Edited by: 887974 on Sep 27, 2011 10:44 PM
  • 1. Re: How to call a WLST from JAVA
    RenévanWijk Oracle ACE
    Currently Being Moderated
    You can use the class weblogic.management.scripting.utils.WLSTInterpreter

    An example is given here: http://download.oracle.com/docs/cd/E12840_01/wls/docs103/config_scripting/using_WLST.html

    Look for the 'Embedded Mode' section.
  • 2. Re: How to call a WLST from JAVA
    890977 Newbie
    Currently Being Moderated
    Is there any java api, which can the WLST scipt ????


    Thanks
    Alok
  • 3. Re: How to call a WLST from JAVA
    Kavin Newbie
    Currently Being Moderated
    import org.python.util.InteractiveInterpreter;
    
    import weblogic.management.scripting.utils.WLSTInterpreter;
    
    public class WLSTUtil {
        private InteractiveInterpreter interpreter;
    
        public WLSTUtil() {
            super();
            interpreter = new WLSTInterpreter();
        }
    
        public boolean validateConnection(String weblogicUser, String weblogicPassword, String weblogicURL) throws Exception {
            try {
                this.connect(weblogicUser, weblogicPassword, weblogicURL);
                this.disConnect(this.interpreter);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
        }
    
        public void disConnect(InteractiveInterpreter interpreter) {
            StringBuffer buffer = new StringBuffer();
            buffer.append("disconnect(force='true')\n");
            interpreter.exec(buffer.toString());
        }
    
        public void connect(String weblogicUser, String weblogicPassword,
                            String weblogicURL) {
            StringBuffer buffer = new StringBuffer();
            buffer.append("connect('" + weblogicUser + "','" + weblogicPassword +
                          "','" + weblogicURL + "')");
            this.interpreter.exec(buffer.toString());
        }
    
        public void undeployApp(String appName) throws Exception {
            try {
    
                StringBuffer wlstCommand = new StringBuffer();
                wlstCommand.append("cd('/')\n");
                wlstCommand.append("cmo.lookupApplication('" + appName + "')\n");
                wlstCommand.append("undeploy('" + appName + "')\n");
    
                interpreter.exec(wlstCommand.toString());
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
        }
    
        public static void main(String[] args) {
            WLSTUtil wlstUtil = new WLSTUtil();
        }
    
    }
  • 4. Re: How to call a WLST from JAVA
    user721330 Newbie
    Currently Being Moderated
    Kavin,

    I am trying to use this java code from command line and getting following error during compilation

    WLSTUtil.java:3: package org.python.util does not exist
    import org.python.util.*;
    ^
    WLSTUtil.java:5: package org.python.util does not exist
    import org.python.util.InteractiveInterpreter;

    I am assuming it is classpath issue. Could you advise on change needed to make it work.
  • 5. Re: How to call a WLST from JAVA
    Assaf.Lowenstein Newbie
    Currently Being Moderated
    How would you extend your sample to add/delete users?

    Thanks, Assaf

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points