Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4340292
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:19:46+00:00 2026-05-21T11:19:46+00:00

I am calling the below command using java This is the java initialization String

  • 0

I am calling the below command using java

This is the java initialization

String fileName="C:\\temp\\A  a.txt";
String  sCmd = "cmd /c start \"\" \"" + fileName + "\"";

This is what I get when I print sCmd

 cmd /c start '" 'C:\temp\A   a.txt'

This is how I run the command

 Runtime.getRuntime().exec(sCmd);

The file name contains multiple spaces and when I run this command from Java its throwing an error because its not recognizing the multiple spaces.It works when no space or one space is there?How to handle files with multiple spaces through windows command

Sample java program

   import java.io.File;
import java.io.IOException;

public class A
{
    public static void main(String[] args)
    {
        String fileName = "C:\\temp\\a  dfdfd   f.txt";
        File file = new File(fileName);
        String sCmd = "cmd /c start \"\" \"" + file.getAbsolutePath() + "\"";

        System.out.println("exec cmd=<" + sCmd + ">");
        try
        {
            Runtime.getRuntime().exec(sCmd);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

This gives me error when from windows side when I run the java code.

This is the java output

exec cmd=<cmd /c start "" "C:\temp\a  dfdfd   f.txt">

and I am running in Windows XP further this is not opening any file leave aside one with spaces.

Solution:

import java.io.File;
import java.io.IOException;

public class A
{
    public static void main(String[] args)
    {
        // String fileName = "C:\\temp\\a.txt";
        String fileName = "C:\\temp\\a  dfdfd   f.txt";
        File file = new File(fileName);
        String sCmd = "cmd /c start \"\" \"" + file.getAbsolutePath() + "\"";

        System.out.println("exec cmd=<" + sCmd + ">");
        try
        {
            Runtime.getRuntime().exec(sCmd.split(" "));
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:19:47+00:00Added an answer on May 21, 2026 at 11:19 am

    cmd /c start "C:\temp\A a.txt" works from the command line.
    You would need to escape the double quotes in the above command with a back slash if this command is called via Java’s Runtime.getRuntime().exec(...)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's wrong with this code?I'm currently calling the code below using a link. Is
I'm calling a python script from the below one using subprocess. From the command
I'm calling the code below. On the line (IDataReader dr = cmd.ExecuteReader()) sql barfs
when calling a SSIS package (C# app) using LoadFromSqlServer, does the user account have
I have an extremely simple script with PHP exec , calling mysql command: $dbhost
I am using the below class to hide and show the windows task bar.
I am calling an SSIS package remotely using a stored procedure and a call
In the script below I am calling a batch file to break mirroring between
I have got a crash at calling function Clearrendertargetview(). Below is the code: HWND
The loop below is callign a proc that does various 'things' If it should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.