Runtime.exec() - using cp and files with spaces on unix

I'm using the Runtime.exec method to copy files in a unix environment. This works fine until there is a file where the name has spaces in it. I've read the article on the pitfalls of using runtime and how it breaks a string on white spaces and this is what is happening. I've also found another topic that was having the same problem, but they were using /usr/bin/dos2unix. I've tried putting quotes around the filename, but it still breaks on the first space. Any suggestions on how to get around this or another way of doing this would be greatly appreciated.
An example of the os command string is:
/usr/bin/cp /tmp/file with space.doc /docs
Thanks!

Hi!
Well I dont have any Sun machine right here to try this but in windows It works great.
Have you tried something like this ?
import java.io.*;
public class OSCopy {
    public static void main(String[] args) {
        try {
            String space = " ";
            String copycmd = "E:\\cp.cmd";
            String source = "E:\\File with space.txt";
            String destination = "E:\\tmp";
            String cmd = copycmd + space + "\"" + source + "\"" + space + destination;
            System.out.println("cmd: " + cmd);
            Runtime runtime = Runtime.getRuntime();
            Process copy = runtime.exec( cmd );
            BufferedReader reader = new BufferedReader( new InputStreamReader( copy.getInputStream()) );
            String line = null;
            while( (line = reader.readLine()) != null ) {
                System.out.println( line );
        catch (Exception e) {
            e.printStackTrace();
cp.cmd is a simple dos copy command
copy %1 %2
*/good luck!

Similar Messages

  • Using Runtime exec() to open and close process like java or javac

    Hi, I m a student who is learning IT and is wondering if
    Runtime exec() can run and stop commands like java and javac?
    Can someone post the code to do so here?Thank you

    Well, Here is my complete code:
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    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);
    BufferedReader br = new BufferedReader(isr);
    String line=null;
    while ( (line = br.readLine()) != null)
    System.out.println(type + ">" + line);
    } catch (IOException ioe)
    ioe.printStackTrace();
    class Program implements Runnable
         Process proc;
    String args[];
         String[] cmd = new String[4];
         boolean isGone=false;
         public Program(String args[])
              this.args=args;
         public void run()
              if (isGone==true)
                   try
                        Runtime.getRuntime().exec(cmd).destroy();
                   catch(IOException e)
                        System.out.println("Error: "+e.toString());
                   System.out.println("User abort");
         public void start(String args[])
              if (args.length < 1)
    System.out.println("USAGE: java GoodWindowsExec <cmd>");
    System.exit(1);
    try
    String osName = System.getProperty("os.name" );
    if( osName.equals( "Windows NT" ) )
    cmd[0] = "cmd.exe" ;
    cmd[1] = "/C" ;
    cmd[3] = args[0];
    else if( osName.equals( "Windows 95" ) ||osName.equals( "Windows 98" ))
    cmd[0] = "command.com" ;
    cmd[1] = "/C" ;
    cmd[2] = args[0];
                        cmd[3]=args[1];
    Runtime rt = Runtime.getRuntime();
    // System.out.println("Execing " + cmd[0] + " " + cmd[1]
    // + " " + cmd[2]+" "+args[1]);                                    
    proc = rt.exec(cmd);
    // 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();
         public void destroy()
              isGone=true;
    class test1 implements ActionListener
         String s[]={"c:\\jdk1.3\\bin\\java.exe","ContactProcessor1"};
         GUI g=new GUI();
              Program p=new Program(s);
         public test1()
              //didnt work
              g.getStart().addActionListener(this);
              g.getStop().addActionListener(this);
              g.show();
         public void actionPerformed(ActionEvent e)
              if (e.getSource()==g.getStart())
                   p.start(s);
                   p.run();
              if (e.getSource()==g.getStop())
                   p.destroy();
                   p.run();
         public static void main(String args[])
              test1 t = new test1();
    class GUI extends JFrame
         JButton start;
         JButton stop;
         public GUI()
              super();
              getContentPane().setLayout(new FlowLayout());
              start=new JButton("start");
              stop=new JButton("stop");
              getContentPane().add(start);
              getContentPane().add(stop);
              setSize(100,100);
         public JButton getStart()
              return start;
         public JButton getStop()
              return stop;
    }

  • How to delete the file with space in name

    Hi
    I want to delete the file "test ex.txt" file.
    i run the following command in command prompt.i can delete the file successfully.
    /bin/rm -f /mnt/"test ex.txt"
    I want to run the command from java.So i am using the following code
    String cmd = "/bin/rm -f /mnt/\"test ex.txt\"";
         Runtime rt = Runtime.getRuntime();
    process = rt.exec(cmd);
    The file was not deleted.
    How to delete the file with space in name?
    Help me

    Use the form of exec that takes an array of command + args.
    arr[0] = "/bin/rm"
    arr[1] = "-f"
    arr[2] = "/home/me/some directory with spaces";Or use ProcessBuilder, which is the preferred replacement for Runtime.exec, and which Runtime.exec calls.

  • MY system folder, including hidden folders are littered with duplicate folders and files with the suffix (from old Mac) How do I remove them

    I have a Macbook Pro running Leopard 10.5.8. I had a problem with my my operating system (my fault, I moved a file I shoudnt have) couldnt boot up but was able to boot up from a backup. I managed to repair my original system except now all the system folders, including hidden folders are littered with duplicate folders and files with the suffix (from old Mac).  For the most part the dupes are an exact copy, but not always.  I want to remove them to free up space and cant imagine duplicate folders in the /system/library are not hindering my computer. But I dont know where to start and am afraid of doing irreparable damage. Any ideas

    pacull,
    Use iCal>View>Show Notifications to choose what to do with the notification.

  • What are the best storage systems to archive inactive (but still used) apps and files?

    What are the best storage systems to archive inactive (but still used) apps and files of all types? I already have a Time Casule which I back up on and I'm needing to create more space in my flash drive in my Mac Pro, so I need another external storage system I can use to archive all that I don't currently need. I may still wish to edit and use the files, and I am not very techy and am a broke student... so anything grossly expensive is a no-no for me (between $50 and $200 is idyllic). If you could simply post links of different good/useful hard-drives that I can use, I'd be thankful.
    Thank you
    p.s. I'm australian, so anything within this country would be great. Or I can deal with somewhere that has cheap shipping.   

    It's been moved and seconded. Any discussion? Yes:
    Buy two external drives. Use one for Time Machine. Be sure this drive is at least twice the capacity of your computer's hard drive. Use the second drive as a bootable clone of your computer's hard drive. Should something happen you can immediately get the computer up and running from the clone until your restore from the Time Machine backup. This is what is called redundancy. Should either backup drive fail, there's another to take its place. Backup redundancy is very important. You can never have too many backups.

  • After installing latest update Realplayer recording no longer works, I think Mozila is using a library file with the same name

    After installing the latest update to Firefox onto Vista operating system the record part of real player no longer works, I think Mozila and Real are using a library file with the same name and the Mozila update is overwriting a Realplayer library file.

    I was giong to say, "Help me, Adobe Joe Kenobi", but it looks like you're growing a beard waiting for an answer, too.
    The same thing happened to me today. I trusted the "update" message since it was from Adobe, and apprently did so at my own peril. Now I get the "failed to load Core dll" message with a secondary reference to a memory address.
    Just an observation, but what good are these forums when there are no answers? I see this problem as "Caused by Adobe Update", and would expect them to troublshoot and offer up a remedy. Since you've been waiting over two months with no response, I suppose it's time to trek off wasting more of my precious time to fix their problem. If I find the solution, I'll post it back to you.
    May the Force be with you.
    Mark

  • Configure CRS2008 to using AD and Kerberos with Java application servers.

    Hi All,
    I have configure CRS2008 to using AD and Kerberos with Java application servers. Domain Controller is installed on W2K3 Server. In addition, CRS2008 is installed on another W2k3 Server.
    I have create service account in domain controller: CMSACC
    I have create two user account: CRuser1 and CRuser2
    I have create domain group: CRSGroup
    After I had run the setspn in domain controller,I got the message at below:
    Registered ServicePrincipalNames for CN=CMSACC, OU=TEST, DC=BD, DC=com:
        BOBJCentralMS/BDMGTSRV.BD.com
    CMC Setting:
    AD Administration Name: BD\administrator
    Default AD Domain: BD.com
    Add AD Group(Domain\Group): secWinAD:CN=CRSGroup,OU=TEST,D=BD,DC=com
    Service principal name:BOBJCentralMS/CMSACCatBD.com
    I have create a WINNT folder in root directory.Moreover and save bcsLognin.conf and Krb5.ini at here.
    bscLogin.conf:
    com.businessobjects.security.jgss.initiate {
    com.sun.security.auth.module.Krb5LoginModule required;
    krb5.ini:
    [libdefaults]
    default_realm = BD.com
    dns_lookup_kdc = true
    dns_lookup_realm = true
    [realms]
    forwardable = true
    BD.com = {
    default_domain = BD.com
    kdc = BDMGTSRV.BD.com
    I have tested the Kerberos,using kinit CMSACCatBD.com password, and got error message at below:
    Exception: krb_error 41 Message stream modified (41) Message stream modified
    KrbException: Message stream modified (41)
            at sun.security.krb5.KrbKdcRep.check(KrbKdcRep.java:53)
            at sun.security.krb5.KrbAsRep.<init>(KrbAsRep.java:96)
            at sun.security.krb5.KrbAsRep.getReply(KrbAsRep.java:486)
         at sun.security.krb5.KrbAsRep.getReply(KrbAsRep.java:444)
         at sun.security.krb5.internal.tools.Kinit.sendASRequest(Kinit.java:310)
         at sun.security.krb5.internal.tools.Kinit.<init>(Kinit.java:259)
         at sun.security.krb5.internal.tools.Kinit.main(Kinit.java:106)
    My problem is failed to logon CMC and infoview and got error message at below:
    Account Information Not Recognized: Active Directory Authentication failed to log you on. Please contact your system administrator to make sure you are a member of a valid mapped group and try again. If you are not a member of the default domain, enter your user name as UserNameatDNS_DomainName, and then try again.
    Actually, I am sucessful to logon Business View manager with CRuser1. However, I fail to logon CMC and infoview and got the above error. Have you any suggestion to solve this problem?
    Ken.

    if you can logon with client tools then that should be an indication that the service account running the CMS IS working! Good news.
    So the problem is likely with the java portion (krb5/bsclogin or java options)
    If the files are in c:\winnt\ (if not copy them there) and perform c:\program files\business objects\javasdk\bin\kinit username
    then enter and password/enter again
    Probably get the same message. To note in your krb5.ini all domain info must be in CAPS (the .com appears to be in lower case)
    kinit works with just the krb5.ini, java SDK and AD (removing BO config and the service account from the picture). Once that works if your java options are specified properly you should be able to login to CMC/infoview.
    also 1 last point. Add udp_preference_limit = 1 to the krb5 lib defaults section
    libdefaults
    default_realm = BD.com
    dns_lookup_kdc = true
    dns_lookup_realm = true
    udp_preference_limit = 1
    Regards,
    Tim

  • Using .htc behaviour file with Business Catalyst server

    Hi,
    I want to use a .htc behaviour polyfill to add a functionality to old IEs. Is Business Catalyst able to handle that file extension? If not, can I use a .htaccess file with BC to add that extension as mentioned here:
    https://github.com/Schepp/box-sizing-polyfill
    Regards, Dennis

    Some good javascript based sollutions been around for a while and got better and better that achieve all this so always used those but bother less and less now.
    If your on Ie7/8 then you get square! your own fault, lol.
    Never saw no reason for the htc file not to work but guess its not accepted on the server setup.

  • Can I use my Elements8 files with Elements11?

    Can I Use my elements8 files with elements 11?

    When you install PSE 11 it will convert your PSE 8 catalogue to PSE 11. Your PSE 8 catalogue will still be available to use with PSE 8. Once you have a PSE 11 catalogue allyour pictures will be pointed to by that catalogue and available. I think this answers your question, if not post back.

  • [Solved] Removing files with space in name.

    Hello!
    I have files named Feh wallpaper (exactly with space in name) and I was trying to remove it but i don't have any file manager and I remove files by rm.
    I was try rm -r feh_wallpaper and rm feh wallpaper and rm can't remove it
    Last edited by SpeedVin (2009-06-30 04:23:29)

    sHyLoCk wrote:
    SpeedVin wrote:Hello again i have a question how to copy files with space in neme i was trying /file but no succes and i have () in the name of file that's are a problem
    I can't rename it i got the same error when i want copy this file
    Did you try with quotes? and it is \file "\" is an escape sequence.
    Ohh thanks option with quotes work
    Last edited by SpeedVin (2009-07-15 11:23:16)

  • How can i transfer my rarely used applications and files so i can make room on my computer harddrive?

    how can i transfer my rarely used applications and files so i can make room on my computer harddrive?

    Well the applications you should leave alone as they install files elsewhere that are needed like preferences etc that won't be on the external drive if you attempt to run it from there.
    Some programs are completely self contained and will run just fine from a external drive, but applicaiton's are usually small.
    Now your user files! That could use some thinning for sure! Especially stuff you don't use that often and Video files which take up a lot of space.
    You can buy a external USB drive at any office / computer supply store and just plug it in and backup files via drag and drop.
    What happens when you plug in a drive is TimeMachine will pop up and ask to make it a TimeMachine drive, this just backups up everything on your computer to restore from in case your OS X or boot drive fails. It's a image of your drive and so whatever is on the boot drive is on the TM drive. So that doesn't server your purpose which is you want to free some space off your main boot drive.
    However you should get another drive and allow TM to do it's thing to that drive, also to make a hold option bootable clone of your boot drive (using yet another external drive) using Carbon Copy Cloner or SuperDuper so in case your boot drive fails you can quickly and easily boot off the clone and have a working machine right away.
    One of the posters here has a excellent instructions what does what, both should be used to backup your data.
    http://web.me.com/pondini/Time_Machine/Clones.html

  • How do I use Qt and OpenGL with Visual Studio

    Hi! I mainly want to program in C++ and I want to use Qt and OpenGL with Visual Studio.
    I am currently revising C++ and later on i am going to start reading Qt and OpenGL. I have a background of
    Embedded firmware design(C and Assembly).
    The Visual Studio Version I have is 2013 ultimate. How do I use Qt and OpenGL with Visual Studio?
    Thanks
    Alexandros

    Hi ClassicalGuitar,
    The forum supports VS setup and installation. And your issue is not about the forum. I will move the thread to off-topic forum. Thanks for your understanding.
    Regards,
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

  • Export Report to flat file with spaces

    I have a report that is several columns wide. It queries data from our SQL based accounting package to create a flat file with spaces(must be accurate). When we go export the file we don't get all of the columns. Any ideas?

    Hi,
    Are you exporting to CSV or Excel File?
    Export to Excel.
    Bashir Awan

  • Using ConnBean and CursorBean with a Data Source

    Hi all,
    I',m making a web app. using the Jdev (RUP4) with OA Extension. I looked in the help menu under "About Data-Access JavaBeans and Tags". Here I found the following example, but for some stange reason I cannot get the setProperty to work.
    Example: Using ConnBean and CursorBean with a Data Source This following is a sample JSP page that uses ConnBean with a data source to open a connection, then uses CursorBean to execute a query.
    <%@ page import="java.sql.*, oracle.jsp.dbutil.*" %>
    <jsp:useBean id="cbean" class="oracle.jsp.dbutil.ConnBean" scope="session">
    <jsp:setProperty name="cbean" property="dataSource"
    value="<%=request.getParameter("datasource")%>"/>
    </jsp:useBean>
    <% try {
    cbean.connect();
    String sql="SELECT ename, sal FROM scott.emp ORDER BY ename";
    CursorBean cb = cbean.getCursorBean (CursorBean.PREP_STMT, sql);
    out.println(cb.getResultAsHTMLTable());
    cb.close();
    cbean.close();
    } catch (SQLException e) {
    out.println("<P>" + "There was an error doing the query:");
    out.println("<PRE>" + e + "</PRE>\n<P>"); }
    %>
    Does anyone know how to set the "Property" to a datasource and make it work?
    Best regards,
    MHCI

    There is no point-and-click (Import Data Source Metadata) way to use an LDAP server as a datasource. You have to use the Java Function provided on dev2dev. If you need help with it, please post here.
    - Mike

  • How to use audio and mic with multipoint server with out it crashing on you

    how to use audio and mic with multipoint server with out it crashing on you when i use them to make calls thru my station (zero client) at the same time it crashes on me and i need to restart the multipoint is there a setting i may have mist that enable's
    them to work at the same time and if posible can i use a non USB device 

    Look for "Single Sign-on Using Kerberos in Java" in google or on Sun's web site. Maybe this paper will help you.
    Claude

Maybe you are looking for