Sponsored Content
Top Forums UNIX for Dummies Questions & Answers running unix command from java Post 302668407 by madhu_sharan on Monday 9th of July 2012 08:21:16 AM
Old 07-09-2012
Code:
private boolean executeShell(String strShellCmd) {
        try {
            procTemp=runEnv.exec(strShellCmd);

            //read the output from shell console
            BufferedReader brShellOP = new BufferedReader(
                    new InputStreamReader(procTemp.getInputStream()));
            String strShellOP = null;
            while ((strShellOP = brShellOP.readLine()) != null) {
                System.out.println(strShellOP);
            }
            brShellOP.close();

            // catch error from shell console (if any)
            BufferedReader brShellError = new BufferedReader(
                    new InputStreamReader(procTemp.getErrorStream()));
            String strShellError = null;
            while ((strShellError = brShellError.readLine()) != null) {
                System.out.println(strShellError);
            }
            brShellError.close();

            // wait for the process to finish
            try {
                intExitVal = procTemp.waitFor();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
            //returns false if shell console throws error
            if(intExitVal!=0){
                return false;
            }
            return true;

        } catch (IOException ioEx) {
            System.out.println("error in executeShell().IOException");
            ioEx.printStackTrace();
            return false;
        }
    }

this is the entire method that does the execute operation.
Let me know if you require any other inputs for getting my issue solved.

PS: i'm new to this executing-unix-from-java thing.

Thanks a tonne,
Madhu.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Java program calling a UNIX command

I was wondering if it was possible to call a unix command directly from a Java program during run-time. This command could very very basic e.g. "ps -ef" returned as a string, all I need is a starting place so if anyone has any suggestion or examples I would be very grateful (2 Replies)
Discussion started by: QUartz Ite
2 Replies

2. UNIX for Dummies Questions & Answers

Executing UNIX command from java on NT

Hi - I am totally new to UNIX so please bear with me... I run a java program on Win NT server to do file ftp to UNIX server. I log in, cd, create ftp file on UNIX and quit from my java progam - all works well. Now I want to execute a script on UNIX. At the UNIX 'console'/'shell' (!?) you... (1 Reply)
Discussion started by: CJ Walt
1 Replies

3. Shell Programming and Scripting

execute command unix with java:possible or not?

hi everybody, i have a small unix script and i want to use it with java, so how can i executte unix command with java (if it is possible): in fact i want to use some values returned by this script with java swings. is it possible (of course everything is possible) but how can i do? thank you in... (4 Replies)
Discussion started by: mips
4 Replies

4. Shell Programming and Scripting

empty space in java command through unix

I need to pass an empty space in the command ="java ......... " as one of the arguments where java calls some java command.... (It accepts it in windows as " ") Thanks in advance SD (2 Replies)
Discussion started by: shweta_d
2 Replies

5. Programming

exit status running java classpath in unix shell

I have a java classpath running inside of a unix shell script. During my testing it will error with lines that show an example like this below. java.io.FileNotFoundException error at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:129), ... (2 Replies)
Discussion started by: mmcds
2 Replies

6. Programming

execute unix command from java eclips

Hi all, I tried to execute the command by 2 ways : 1) Java Runtime class exec method - but the problem there is that it only can execute scripts without arguments(I need with args) 2) the Jtux classs Uprocess- the problem it is not supported by windows and my eclips platform is on... (0 Replies)
Discussion started by: katzs500
0 Replies

7. Programming

Exception Signal 11 while running JAVA code in UNIX

Hi, when i compile my java code in UNIX using javac, the class file is getting created. But when i try to run the code using java - classpath command, i get the following error. ---------------------------------------- /u/up11/sample/request:>java -classpath /u/up11/sample/request... (0 Replies)
Discussion started by: satish2712
0 Replies

8. UNIX for Advanced & Expert Users

running unix command from java

Hi All, I have been struggling for a week trying to run a unix command from my java program. the unix command is = ssh dmdev3@tsapid01-zt3d01 ':> /t3/envs/dmdev3/test/file_list.txt' when i try to run this command directly on my unix console, it works perfectly. but when i try it form my... (0 Replies)
Discussion started by: madhu_sharan
0 Replies

9. UNIX for Advanced & Expert Users

Command to check java file in UNIX

Guys, i need to check whether java file can be executed in my current unix system or not. Can anyone please help me to get the correct command to check this. My Unix system is HP-UX. Thanks, (1 Reply)
Discussion started by: AraR87
1 Replies

10. Shell Programming and Scripting

Command to get exact tomcat process I am running ignoring other java process

Team, I have multiple batchjobs running in VM, if I do ps -ef |grep java or tomcat I am getting multiple process list. How do I get my exact tomcat process running and that is unique? via shell script? (4 Replies)
Discussion started by: Ghanshyam Ratho
4 Replies
guestfs-java(3) 					      Virtualization Support						   guestfs-java(3)

NAME
guestfs-java - How to use libguestfs from Java SYNOPSIS
import com.redhat.et.libguestfs.*; GuestFS g = new GuestFS (); g.add_drive_opts ("disk.img", null); g.launch (); DESCRIPTION
This manual page documents how to call libguestfs from the Java programming language. This page just documents the differences from the C API and gives some examples. If you are not familiar with using libguestfs, you also need to read guestfs(3). CLOSING THE HANDLE The handle is closed when it is reaped by the garbage collector. Because libguestfs handles include a lot of state, it is also possible to close (and hence free) them explicitly by calling the "close" method. EXCEPTIONS Errors from libguestfs functions are mapped into the "LibGuestFSException" exception. This has a single parameter which is the error message (a "String"). Calling any method on a closed handle raises the same exception. EXAMPLE 1: CREATE A DISK IMAGE // Example showing how to create a disk image. import java.io.*; import java.util.Map; import java.util.HashMap; import com.redhat.et.libguestfs.*; public class CreateDisk { static String output = "disk.img"; public static void main (String[] argv) { try { GuestFS g = new GuestFS (); // Create a raw-format sparse disk image, 512 MB in size. RandomAccessFile f = new RandomAccessFile (output, "rw"); f.setLength (512 * 1024 * 1024); f.close (); // Set the trace flag so that we can see each libguestfs call. g.set_trace (true); // Set the autosync flag so that the disk will be synchronized // automatically when the libguestfs handle is closed. g.set_autosync (true); // Attach the disk image to libguestfs. Map<String, Object> optargs = new HashMap<String, Object>() { { put ("format", "raw"); put ("readonly", Boolean.FALSE); } }; g.add_drive_opts (output, optargs); // Run the libguestfs back-end. g.launch (); // Get the list of devices. Because we only added one drive // above, we expect that this list should contain a single // element. String[] devices = g.list_devices (); if (devices.length != 1) throw new Error ("expected a single device from list-devices"); // Partition the disk as one single MBR partition. g.part_disk (devices[0], "mbr"); // Get the list of partitions. We expect a single element, which // is the partition we have just created. String[] partitions = g.list_partitions (); if (partitions.length != 1) throw new Error ("expected a single partition from list-partitions"); // Create a filesystem on the partition. g.mkfs ("ext4", partitions[0]); // Now mount the filesystem so that we can add files. g.mount_options ("", partitions[0], "/"); // Create some files and directories. g.touch ("/empty"); String message = "Hello, world "; g.write ("/hello", message.getBytes()); g.mkdir ("/foo"); // This one uploads the local file /etc/resolv.conf into // the disk image. g.upload ("/etc/resolv.conf", "/foo/resolv.conf"); // Because 'autosync' was set (above) we can just close the handle // and the disk contents will be synchronized. You can also do // this manually by calling g#umount_all and g#sync. // // Note also that handles are automatically closed if they are // reaped by the garbage collector. You only need to call close // if you want to close the handle right away. g.close (); } catch (Exception exn) { System.err.println (exn); System.exit (1); } } } EXAMPLE 2: INSPECT A VIRTUAL MACHINE DISK IMAGE // Example showing how to inspect a virtual machine disk. import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import com.redhat.et.libguestfs.*; public class InspectVM { static final Comparator<String> COMPARE_KEYS_LEN = new Comparator<String>() { public int compare (String k1, String k2) { return k1.length() - k2.length(); } }; public static void main (String[] argv) { try { if (argv.length != 1) throw new Error ("usage: InspectVM disk.img"); String disk = argv[0]; GuestFS g = new GuestFS (); // Attach the disk image read-only to libguestfs. Map<String, Object> optargs = new HashMap<String, Object>() { { //put ("format", "raw"); put ("readonly", Boolean.TRUE); } }; g.add_drive_opts (disk, optargs); // Run the libguestfs back-end. g.launch (); // Ask libguestfs to inspect for operating systems. String roots[] = g.inspect_os (); if (roots.length == 0) throw new Error ("inspect_vm: no operating systems found"); for (String root : roots) { System.out.println ("Root device: " + root); // Print basic information about the operating system. System.out.println (" Product name: " + g.inspect_get_product_name (root)); System.out.println (" Version: " + g.inspect_get_major_version (root) + "." + g.inspect_get_minor_version (root)); System.out.println (" Type: " + g.inspect_get_type (root)); System.out.println (" Distro: " + g.inspect_get_distro (root)); // Mount up the disks, like guestfish -i. // // Sort keys by length, shortest first, so that we end up // mounting the filesystems in the correct order. Map<String,String> mps = g.inspect_get_mountpoints (root); List<String> mps_keys = new ArrayList (mps.keySet ()); Collections.sort (mps_keys, COMPARE_KEYS_LEN); for (String mp : mps_keys) { String dev = mps.get (mp); try { g.mount_ro (dev, mp); } catch (Exception exn) { System.err.println (exn + " (ignored)"); } } // If /etc/issue.net file exists, print up to 3 lines. String filename = "/etc/issue.net"; if (g.is_file (filename)) { System.out.println ("--- " + filename + " ---"); String[] lines = g.head_n (3, filename); for (String line : lines) System.out.println (line); } // Unmount everything. g.umount_all (); } } catch (Exception exn) { System.err.println (exn); System.exit (1); } } } SEE ALSO
guestfs(3), guestfs-examples(3), guestfs-erlang(3), guestfs-ocaml(3), guestfs-perl(3), guestfs-python(3), guestfs-recipes(1), guestfs-ruby(3), <http://libguestfs.org/>, <http://caml.inria.fr/>. AUTHORS
Richard W.M. Jones ("rjones at redhat dot com") COPYRIGHT
Copyright (C) 2011 Red Hat Inc. <http://libguestfs.org/> The examples in this manual page may be freely copied, modified and distributed without any restrictions. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA libguestfs-1.18.1 2013-12-07 guestfs-java(3)
All times are GMT -4. The time now is 07:20 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy