Run Basic Command-line Java Programs in Sandvox

Is it possible to put a command-line Java program on a website in Sandvox and make it run?

See: documentation for designers

Similar Messages

  • Problem while running dos command from java program

    Dear friends,
    I need to terminate a running jar file from my java program which is running in the windows os.
    For that i have an dos command to find process id of java program and kill by using tskill command.
    Command to find process id is,
    wmic /output:ProcessList.txt process where "name='java.exe'" get commandline,processid
    This command gives the ProcessList.txt file and it contains the processid. I have to read this file to find the processid.
    when i execute this command in dos prompt, it gives the processid in the ProcessList.txt file. But when i execute the same command in java program it keeps running mode only.
    Code to run this command is,
    public class KillProcess {
         public static void main(String args[]) {
              KillProcess kProcess = new KillProcess();
              kProcess.getRunningProcess();
              kProcess = new KillProcess();
              kProcess.readProcessFile();
         public void getRunningProcess() {
              String cmd = "wmic /output:ProcessList.txt process where \"name='java.exe'\" get commandline,processid";
              try {
                   Runtime run = Runtime.getRuntime();
                   Process process = run.exec(cmd);
                   int i = process.waitFor();
                   String s = null;
                   if(i==0) {
                        BufferedReader stdInput = new BufferedReader(new
                               InputStreamReader(process.getInputStream()));
                        while ((s = stdInput.readLine()) != null) {
                         System.out.println("--> "+s);
                   } else {
                        BufferedReader stdError = new BufferedReader(new
                               InputStreamReader(process.getErrorStream()));
                        while ((s = stdError.readLine()) != null) {
                         System.out.println("====> "+ s);
                   System.out.println("Running process End....");
              } catch(Exception e) {
                   e.printStackTrace();
         public String readProcessFile() {
              System.out.println("Read Process File...");
              File file = null;
              FileInputStream fis = null;
              BufferedReader br = null;
              String pixieLoc = "";
              try {
                   file = new File("ProcessList.txt");
                   if (file.exists() && file.length() > 0) {
                        fis = new FileInputStream(file);
                        br = new BufferedReader(new InputStreamReader(fis, "UTF-16"));
                        String line;
                        while((line = br.readLine()) != null)  {
                             System.out.println(line);
                   } else {
                        System.out.println("No such file");
              } catch (Exception e) {
                   e.printStackTrace();
              return pixieLoc;
    }     when i remove the process.waitFor(), then while reading the ProcessList.txt file, it says "No such file".
    if i give process.waitFor(), then it's in running mode and program is not completed.
    Colud anyone please tell me how to handle this situation?
    or Is there anyother way to kill the one running process in windows from java program?
    Thanks in advance,
    Sathish

    Hi masijade,
    The modified code is,
    class StreamGobbler extends Thread
        InputStream is;
        String type;
        StreamGobbler(InputStream is, String type)
            this.is = is;
            this.type = type;
        public void run()
            try
                InputStreamReader isr = new InputStreamReader(is, "UTF-16");
                BufferedReader br = new BufferedReader(isr);
                String line=null;
                while ( (line = br.readLine()) != null)
                    System.out.println(type + ">" + line);
                } catch (IOException ioe)
                    ioe.printStackTrace(); 
    public class GoodWindowsExec
        public static void main(String args[])
            try
                String osName = System.getProperty("os.name" );
                String[] cmd = new String[3];
                 if( osName.equals( "Windows 95" ) )
                    cmd[0] = "command.com" ;
                    cmd[1] = "/C" ;
                    cmd[2] = "wmic process where \"name='java.exe'\" get commandline,processid";
                } else {
                    cmd[0] = "cmd.exe" ;
                    cmd[1] = "/C" ;
                    cmd[2] = "wmic process where \"name='java.exe'\" get commandline,processid";
                Runtime rt = Runtime.getRuntime();
                System.out.println("Execing " + cmd[0] + " " + cmd[1]
                                   + " " + cmd[2]);
                Process proc = rt.exec(cmd);
                System.out.println("Executing.......");
                // any error message?
                StreamGobbler errorGobbler = new
                    StreamGobbler(proc.getErrorStream(), "ERROR");           
                          // any output?
              StreamGobbler outputGobbler = new
                    StreamGobbler(proc.getInputStream(), "OUTPUT");
                          // kick them off
                errorGobbler.start();
                outputGobbler.start();
                // any error???
                int exitVal = proc.waitFor();
                System.out.println("ExitValue: " + exitVal);       
            } catch (Throwable t)
                t.printStackTrace();
    }when i execute the above code, i got output as,
    Execing cmd.exe /C wmic process where "name='java.exe'" get commandline,processid
    and keeps in running mode only.
    If i execute the same command in dos prompt,
    CommandLine
    ProcessId
    java -classpath ./../lib/StartApp.jar;./../lib; com.abc.middle.startapp.StartAPP  2468
    If i modify the command as,
    cmd.exe /C wmic process where "name='java.exe'" get commandline,processid  > 123.txt
    and keeps in running mode only.
    If i open the file when program in running mode, no contents in that file.
    If i terminte the program and if i open the file, then i find the processid in that file.
    Can you help me to solve this issue?

  • Please help How can i run Os commands thru Java programs

    Hey,
    I want to stop and restart the linux server thru java program.Is it possible to run the os commands thru java program.
    I had it thru Runtime.getRuntime().exec("*.exe");
    it only runs the exe files.How can run files other than exe files like .bat,com ans shell commands..Any body knows please help with the code..or mail to this address
    [email protected]
    thankyou,
    regards,
    j.mouli

    What about "start command.com /C execute.bat", or using the overload that takes a String[] as argument?
    What if you use the the full path of execute.bat?
    What error code do you get?
    And what comes th linux, I'm not sure... you'll need a shell interpreter there too, me thinks. (never had to run anything with runtime.exec on linux). Check the man pages if csh, bash, ksh, or what ever shell you like.

  • Run shell commands using java program

    Hi guys,
    I am trying to run shell commands like cd /something and ./command with arguments as follows, but getting an exception that ./command not found.Instead of changing directory using "cd" command I am passing directory as an argument in rt,exec().
    String []cmd={"./command","arg1", "arg2", "arg3"};
    File file= new File("/path");
    try{
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(cmd,null,file);
    proc.waitFor();
    System.out.println(proc.exitValue())
    BufferedReader buf = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    catch(Exception e)
    {e.printStackTrace();
    So can anyone please tell me what is wrong with this approach? or is there any better way to do this?
    Thanks,
    Hardik

    warnerja wrote:
    What gives you the idea that the process to execute is called "./command"? If this is in Windows, it is "cmd.exe" for example.It does not have to be cmd.exe in Windows. Any executable or .bat file can be executed as long as one either specifies the full path or the executable is in a directory that is in the PATH.
    On *nix the file has to have the executable bit set and one either specifies the full path or the executable must be in a directory that is in the PATH . If the executable is a script then if there is a hash-bang (#!) at the start of the first line then the rest of the line is taken as the interpreter  to use. For example #!/bin/bash or #!/usr/bin/perl .
    One both window and *nix one can exec() an interpreter directly and then pass the commands into the process stdin. The advantage of doing this is that one can change the environment in one line and it  remains in effect for subsequent line. A simple example of this for bash on Linux is
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    public class ExecInputThroughStdin
        public static void main(String args[]) throws Exception
            final Process process = Runtime.getRuntime().exec("bash");
            new Thread(new PipeInputStreamToOutputStreamRunnable(process.getErrorStream(), System.err)).start();
            new Thread(new PipeInputStreamToOutputStreamRunnable(process.getInputStream(), System.out)).start();
            final Writer stdin = new OutputStreamWriter(process.getOutputStream());
            stdin.write("xemacs&\n");
            stdin.write("cd ~/work\n");
            stdin.write("dir\n");
            stdin.write("ls\n");
            stdin.write("gobbldygook\n"); // Forces output to stderr
            stdin.write("echo $PATH\n");
            stdin.write("pwd\n");
            stdin.write("df -k\n");
            stdin.write("ifconfig\n");
            stdin.write("echo $CWD\n");
            stdin.write("dir\n");
            stdin.write("cd ~/work/jlib\n");
            stdin.write("dir\n");
            stdin.write("cat /etc/bash.bashrc\n");
            stdin.close();
            final int exitVal = process.waitFor();
            System.out.println("Exit value: " + exitVal);
    }One can use the same approach with Windows using cmd.exe but then one must be aware of the syntactic differences between commands running in .bat file and command run from the command line. Reading 'help cmd' s essential here.
    The class PipeInputStreamToOutputStreamRunnable in the above example just copies an InputStream to an OutputStream and I use
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    public class PipeInputStreamToOutputStreamRunnable implements Runnable
        public PipeInputStreamToOutputStreamRunnable(InputStream is, OutputStream os)
            is_ = is;
            os_ = os;
        public void run()
            try
                final byte[] buffer = new byte[1024];
                for (int count = 0; (count = is_.read(buffer)) >= 0;)
                    os_.write(buffer, 0, count);
            } catch (IOException e)
                e.printStackTrace();
        private final InputStream is_;
        private final OutputStream os_;
    }

  • Running OpenSSL command through Java Program

    How do I execute openssl commands through a java program? Any packages or wrapper classes are there? Please help.
    Thanks.

    Hi!
    What do you mean execute commands? Like: "openssl x509 -in cert.pem -out certout.pem" ??
    In that case you can just try the following:
    import java.lang.Runtime;
    try {
    Runtime.getRuntime().exec("openssl x509 -in cert.pem -out certout.pem");
    }catch (Exception e) {
    e.printStackTrace();
    ........

  • Error while running the SAP JCO java program running via command line

    Hi,
        We are facing an issue while using SAP JCO. When i try to run the sample program using RAD 8.0 ( IBM IDE tool For Java Development) its working fine.
    The same sample program if i try to run using command line, Then its giving below exception message.
    Exception in thread "main" java.lang.NoClassDefFoundError: Integration
    NOTE: I have configured proper sapjco jar & Dll files path in class path settings in my batch file.

    Hi,
    class loader can't find class definition during runtime but it could find it during compilation. So the problem is with your classpath. Does your classpath point to file with class Integration? Check this [blog|http://javarevisited.blogspot.com.au/2011/06/noclassdeffounderror-exception-in.html].
    Cheers
    Added reference to blog.

  • Running a command line in Java

    Dear all,
    I have a question, I need to run a command line in a Java program. The commands line is the next:
    cas.exe -i file1
    I have been seeing in internet and I have tried:
    Process ls_proc = Runtime.getRuntime().exec("cmd /c start D:\\cas>cas.exe -i file1");
    it start the window where the cas folder is... but it dont run the commands line : cass.exe -i file1 :(
    Somebody can help me, please??
    Andrea

    try running it like this.
    Process ls_proc = Runtime.getRuntime().exec("cmd /c start D:\\cas>cas.exe -i file1" +"\n");
    if that does not work put this underneath the original.
    ls_proc.newLine();
    or look into the ProcessBuilder class in java.lang.

  • How to run command line argument programe

    Hi guys, I am doing pass command line argument programe in java but I don't know how to run this programe. Path for this programe in my my computer is C:\Users\Desktop\Mainjava\mycode\CommandProgjava*
    {code/}
    public class CommandProg
    public static void main(String[] args)
    System.out.println("d");
    for (int i = 0; i < args.length; i++)
    System.out.println(args);
    {code/}
    Where i need to go and what command i need to give so i can execute this programe(I am using window vista). I only know i have to give
    this command some where CommandProg arg1 arg2 arg3 arg4. Output should be
    Output:
    arg1
    arg2
    arg3
    arg4
    Please help me, Thanks in advance.
    Edited by: JayVirk on Dec 30, 2007 11:33 AM

    Jay,
    Your question isn't very clear, hence Joerg's well meaning but irrelevant advise.
    Do you mean:
    I've written a simple program in java which echos
    it's command-line arguments to back to the console.
    Here's my code:
    package forums;
    public class ArgsEchoer
      public static void main(String[] args) {
        for (String arg : args) {
          System.out.println(arg);
    But can't figure out how to compile and run the program.
    I'm using winblows shista, and it's cr@p.
    Please help me, Thanks in advance.So... where are you at? Have you installed the JDK (java development kit)? Which version? Is your path set? Is your classpath set?
    Start here: http://java.sun.com/developer/onlineTraining/new2java/

  • Running ls command from Java stroed procedure no output

    Hi ,
    I am trying to run ls command from java stored procedure in oracle
    Process p = Runtime.getRuntime().exec("ls");
    BufferedReader stdInput = new BufferedReader(new
    InputStreamReader(p.getInputStream()));
    BufferedReader stdError = new BufferedReader(new
    InputStreamReader(p.getErrorStream()));
    // read the output from the command
    System.out.println("output of the command run:\n");
    while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
    from java stored procedure in oracle.
    i get output of println statments but it does not go into while loop to print from stdInput.
    Result of running Java stored procedure is -
    output of the command run:
    Call completed.
    when i run the program on client side it works fine.
    Has anybody tried this from java stroed procedure.
    Thanks,
    Jag

    Jag,
    Actually, the question of whether it works for me seems to depend on the version of the OS (or Oracle). On RedHat Linux (Oracle 8.1.6) it didn't work at all, but on Solaris (Oracle 9.0.2) it did. Here's the output from that run:
    SQL> /
    output of the command run:
    init.ora
    initDBPart9i.DBPSun01.ora
    initdw.ora
    lkDBPART9I
    orapw
    orapwDBPart9i
    spfileDBPart9i.ora
    Done
    PL/SQL procedure successfully completed.
    But, I did need to change a line of your code to this:
    Process p = Runtime.getRuntime().exec("/usr/bin/ls");
    your original was:
    Process p = Runtime.getRuntime().exec("ls");
    You might consider, if possible, use of some of the Java File classes instead of ls, as this might make things more predictable for you. There were some examples in oramag.com a few months ago, but they were pretty simple (you might not need them).
    Hope this helps,
    -Dan
    http://www.compuware.com/products/devpartner/db/oracle_debug.htm
    Debug PL/SQL and Java in the Oracle Database

  • How to run Perl script in Java program?

    Some say that the following statement can run Perl script in Java program:
    Runtime.getRuntime().exec("C:\\runPerl.pl");
    But when I run it, it'll throw IOException:
    java.io.IOException: CreateProcess: C:\runPerl.pl error=193
    at java.lang.Win32Process.create(Native Method)
    at java.lang.Win32Process.<init>(Win32Process.java:63)
    at java.lang.Runtime.execInternal(Native Method)
    at java.lang.Runtime.exec(Runtime.java:566)
    at java.lang.Runtime.exec(Runtime.java:428)
    at java.lang.Runtime.exec(Runtime.java:364)
    at java.lang.Runtime.exec(Runtime.java:326)
    at test.runPerl.main(runPerl.java:16)
    Exception in thread "main"
    Where is the problem? In fact, I do have a Perl script named "runPerl.pl" in my C:/ directory. Could anybody tell me why it can't be run? Thanks in advance.

    Hello sabre,
    First of all thanks for your reply.
    I have tried like what you mentioned.
    Eventhough, i could get the exact error message.
    I could get the Exception
    "Exception:java.lang.IllegalThreadStateException: process hasn't exited"
    <code>
    import java.io.*;
    public class Sample{
    public static void main(String args[]) {
    String s = null;
    Process p;
    try
         p = Runtime.getRuntime().exec("tar -vf sample.tar");
         //p.waitFor();
         if(p.exitValue() == 0)
              System.out.println("CommandSuccessful");
         else
              System.out.println("CommandFailure");
    catch(Exception ex)
         System.out.println("Exception:"+ex.toString());
    </code>
    In this code, i tried to run the "tar command". In this command i have given -vf instead -xvf.
    But it is throwing
    "Error opening message catalog 'Logging.cat': No such file or directory
    Exception:java.lang.IllegalThreadStateException: process hasn't exited
    CommandFailure"
    "Error opening message catalog 'Logging.cat'" what this line means ie,
    I dont know what is Logging.cat could you explain me its urgent.
    Thanks in advance.
    Rgds
    tskarthikeyan

  • How to run a command line argument

    i want to run a command line argument, say for eg......i want to execute the "dir" command on the dos prompt and capture the output.
    how can i do this.
    actually i want to capture the output of the ping command and write it to a file. can any body plsssssss help

    This command runs a ping to "address" and writes the output back to the command line. To capture it, just put strings into an array or do whatever you want with it. If you want to wait until the program completes before reading its input, use p.waitFor().
    Process p = Runtime.getRuntime().exec("ping " + address);
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String currLine = null;
    while((currLine = in.readLine()) != null)
      System.out.println("ping: " + currLine);
    [/code                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • How to create command line java client

    I have a java servlet FileClient.java and am invoking it thru POST method from the html file. How can I create command line java client so that I can execute this class file from windows command prompt.
    e.g. java FileClient <argument>
    TIA

    > .. there's also a "won't work".
    I could post a sample...I know that you can instantiate and call aservlet,
    or even write your own servlet container - butit's
    still a local instance. He can't invoke his remote
    servlet by calling its class on the client like he
    suggested.HttpURLConnection in main.Only if his Servlet has a main method. Which wouldn't really make it a servlet anymore.
    OP said he wants to invoke it from command-line using
    java -cp . TheServletClass
    He said he has a servlet called FileClient. And he can't invoke a servlet directly that way, without building a wrapper that starts it, making it more than a servlet, and it definitely won't execute on the server. That's my whole point. I never wanted to say you can't write a command-line program that invokes a servlet on a remote server. I do it myself after all.
    I guess I should have expressed myself more clearly.

  • Deploying basic command line to multiple PCs

    Hi,
    I am trying to execute this basic command line to multiple PCs from SCCM.
    netcfg /c s /I MS_Server
    If I execute it locally with elevated permissions, it works fine
    If I execute it locally without elevated permissions, it doesn't work
    When I try to create a package in SCCM to run this command line, it is showing as it succeeded but when I look at the execmgr.log on the client machine, it says it failed with error code 1.
    Someone can give me a hint of what I could try?
    Thank you!

    To my knowledge, you do not need to 'install' File & Printer Sharing, it's just a firewall port configuration on Windows clients. This could be configured through Group Policies.
    Also, your command is for Windows PE as refrenced here:
    http://technet.microsoft.com/pt-pt/library/hh875638%28v=ws.10%29.aspx

  • Clear screen in "Run SQL Command Line" causes the utility to dump in Window

    Just for your notice... I have not checked if i can reproduce this on Linux platform or on second node running Windows XP Pro.
    But if I use "Run SQL Command Line" utility and do
    clear screen
    The utility dumps, (not the database though only the utlility)
    Maybe some other people could confirm if this findings is a bug or not and if it is a port specific issue with Windows XP only.
    Kindly Rgds
    /Ulf, Kentor IT Sweden

    Tracking info is in
    Re: SQL*Plus generated Program Error
    -- cj

  • How to execute dos command in Java program?

    In Perl, it has System(command) to call dos command.
    Does java have this thing?
    or any other way to execute dos command in java program?
    Because i must call javacto compile a file when the user inputed a java file name.

    Look in the Runtime class, it is implemented using the Singleton design pattern so you have to use the static method getRuntime to get its only instance, on that instance you can invoke methods like
    exec(String command) that will execute that command in a dos shell.
    Regards,
    Gerrit.

Maybe you are looking for

  • Cost price in migo

    Hi, We have mapped a specific scenario where stock transfer process are happening between mfg plants to sales plants where billing price (STO price) are plant and material specific. I made the price determination at delivery level by assigning the pr

  • User Profile Syncrhonization Scheduling twice a day

    Client wants to schedule User Profile Synchronization from AD scheduling twice a day at 12.00 AM and 12.00 PM which includes any update in the AD photos also. How can I achieve this ?

  • Webdynpro for ABAP - Popup apearing for userid and pwd

    Hi Experts, We have implemented ESS and gone live. Now we have created one custom application in webdynpro for ABAP and attached with portal. Initially there was a problem, the application was not getting executed because of system configuration, the

  • AIR 2.7.1 breaks sound clip in existing app

    AIR build 19610 has produced strange behavior in an existing app that has been published and installed since AIR 1.5. There is a clip onstage (using Flash CS5) which contains sound on its timeline, as well as some simple timeline code (AS3). Certain

  • Trying to install trial version of Adobe Acrobat Pro XI.

    After downloading it says that the trial already expired when I just downloaded it.