1. Create a Shell File
Login to Linux, create a shell file, change the permissions as below:
# cd /cms
# pwd
/cms
# cat > testshell.sh
#!bin/sh ls -l > KS`date +%d%m%Y%H%M%S`
# chmod 755 testshell.sh
2. Create a java class file as below
Modify host, user, password and command1 variables as per your system.
Note: You must add com.jcraft.jsch_0.1.31.jar file in your java library.
public class RunShell {
public static void shellName()
{
String host="192.168.1.223", user="root", password="ks@123", command1="/cms/testshell.sh";
try{
java.util.Properties config = newjava.util.Properties();
config.put("StrictHostKeyChecking", "no");
com.jcraft.jsch.JSch jsch = newcom.jcraft.jsch.JSch();
com.jcraft.jsch.Session session=jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
com.jcraft.jsch.Channel channel=session.openChannel("exec");
((com.jcraft.jsch.ChannelExec)channel).setCommand(command1);
channel.setInputStream(null);
((com.jcraft.jsch.ChannelExec)channel).setErrStream(System.err);
java.io.InputStream in=channel.getInputStream();
channel.connect();
byte[] tmp=new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
System.out.print(new String(tmp, 0, i));
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
System.out.println("DONE");
}catch(Exception e){
e.printStackTrace();
}
}
}
3. Compile the java file and create its jar file.
4. Copy RunShell.jar, com.jcraft.jsch_0.1.31.jarfiles in C:\Oracle\Middleware\Oracle_FRHome1\forms\java folder
5. Sign the jar file with sign_webutil utility available in C:\Oracle\Middleware\asinst_1\binfolder
6. Edit default.env file available in C:\Oracle\Middleware\user_projects\domains\ClassicDomain\config\fmwconfig\servers\AdminServer\applications\formsapp_11.1.2\config folder and add CLASSPATH variable with C:\Oracle\Middleware\Oracle_FRHome1\forms\java\com.jcraft.jsch_0.1.31.jar;C:\Oracle\Middleware\Oracle_FRHome1\forms\java\RunShellFile.jar;
7. Open Forms Builder
Create canvas, block, push button.
Go to Program Menu -> Import Java Classes… -> Select RunShell class -> Click Import Button.
Now you will file RunShell Package is created in Program Units.
Now write the code in WHEN-BUTTON-PRESSED trigger of push button as :
RunShell.shellName;
Where shellName is the function/method created in RunShell java file and is imported in RunShell Package in Program Units.
Compile and Run.
Now you can check your linux where a new file will be created when you press button.
Enjoy!!!
0 Comments