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

  • SEARCH
  • Home
  • 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 9024569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:03:00+00:00 2026-06-16T06:03:00+00:00

Recently I found myself having to launch the ghostscript command from java, in both

  • 0

Recently I found myself having to launch the ghostscript command from java, in both linux and windows environments, with whitespaces in input/output filenames. An example of the command follows:

gs -q -dNOPAUSE -dBATCH -sDEVICE=pnggray -r300 -sOutputFile=/home/nic/tomcat/6.0.33 with spaces/temp/Thread-11/img-%03d.png /home/nic/tomcat/6.0.33 with spaces/temp/tmpfile.tmp

The gs gets replaced by gswin32 on windows, given that ghostscript is in the Path.

I quickly realized that I had to escape the file names in some manner, so the first thing I’ve done was to enclose them between double quotes. This worked on windows, but not on linux: on linux I’ve tried the double quotes enclosing and also escaping whitespaces with backslashes, but without success.

For launching the command I’m using Runtime.getRuntime().exec(command);, passing one single string. I found the following question getting ghostscript to take in files with spaces in their name (like something in "my documents") but:

  • I desired to extend it also for linux;
  • I found that double quoting works for me, differently than how it’s pointed out there.

I would like to understand this thing once per all: can you help me to do this?

Here follows a summary of my attempts, per SO.

Windows

Enclosing file names in double quotes worked for me:

gswin32 -q -dNOPAUSE -dBATCH -sDEVICE=pnggray -r300 -sOutputFile="C:\Program Files\tomcat 6.0.33 with spaces\temp\Thread-11\img-%03d.png" "C:\Program Files\tomcat 6.0.33 with spaces\temp\tmpfile.tmp"

Linux

Tried to enclose file names in double quotes

gs -q -dNOPAUSE -dBATCH -sDEVICE=pnggray -r300 -sOutputFile="/home/nic/tomcat/6.0.33 with spaces/temp/Thread-11/img-%03d.png" "/home/nic/tomcat/6.0.33 with spaces/temp/tmpfile.tmp"

Tried to escape white space with backslash

gs -q -dNOPAUSE -dBATCH -sDEVICE=pnggray -r300 -sOutputFile=/home/nic/tomcat/6.0.33\ with\ spaces/temp/Thread-11/img-%03d.png /home/nic/tomcat/6.0.33\ with\ spaces/temp/tmpfile.tmp

Tried both together

gs -q -dNOPAUSE -dBATCH -sDEVICE=pnggray -r300 -sOutputFile="/home/nic/tomcat/6.0.33\ with\ spaces/temp/Thread-11/img-%03d.png" "/home/nic/tomcat/6.0.33\ with\ spaces/temp/tmpfile.tmp"
  • 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-06-16T06:03:01+00:00Added an answer on June 16, 2026 at 6:03 am

    Why don’t you use Runtime.exec(String[] args) which takes multiple arguments ? This variant is designed to avoid you having to escape such arguments. Since the arguments are provided separately there’s no space-based interpolation required and thus no confusion.

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

Sidebar

Related Questions

I recently had to re-install Glassfish 3.1.2 from scratch and I found myself spending
I've found myself having to write some VBA code recently and just wondered if
I recently switched from Eclipse to IntelliJ IDEA, and found myself wondering how to
I recently found myself having the same problem Lukasz describes here . While the
I have recently found myself on a Linux computer and am liking it so
Recently, I found myself having to write up some concerns I have about race
I recently found myself writing a piece of code that executed a Core Data
So I recently found myself writing a loop similar to this one: var headers
I have recently found myself becoming more negative about EF and cannot help wondering
I've recently found myself using the following macro with gcc 4.5 in C++11 mode:

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.