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 7581351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:10:52+00:00 2026-05-30T18:10:52+00:00

I’m writing a bit of code to run a shell script using process that

  • 0

I’m writing a bit of code to run a shell script using process that loads and runs a file in terminal. The problem I’m having is getting the filename to be recognised by terminal due to the spaces, for example :

"$ ./run_file.sh foo bar.ss" 

should be run in terminal as

"$ ./run_file.sh foo\ bar.ss"

Heres the code to change it replace it:

JPanel panel1 = new JPanel();
JButton button = new JButton("Run");
button.setAlignmentX( Component.CENTER_ALIGNMENT);

button.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent event){

        run();

    }

});
//button.setAlignmentX(0.5);
panel1.add(button);
panel1.add(Box.createVerticalGlue());
panel1.add(button);

menuB = new JMenuBar();

JMenu dropD = new JMenu("File");
menuB.add(dropD);

JMenuItem loadR = new JMenuItem("Load file");
JMenuItem quit = new JMenuItem("Quit");
dropD.add(loadR);
dropD.add(quit);
loadR.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event) {
            JFileChooser fileopen = new JFileChooser();

            int r= fileopen.showDialog(panel, "Load file");

            if (r == JFileChooser.APPROVE_OPTION) {
                File file = fileopen.getSelectedFile();
                String string = file.toString();
                string = string.replaceAll(" ", "\ ");
                //String output = aa.replaceAll("/",Character.toString(File.separatorChar));
                System.out.println(string);
                loadFile = file;
        }

       }
  });

I have been tried using String.replaceAll but get

java:66: illegal escape character

i realise that I can use File.separatorChar :

string = string.replaceAll(" ", Character.toString(File.separatorChar)+" ");

but this doesn’t seem to replace anything…
Any help would be much appreciated.

Thanks

  • 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-30T18:10:54+00:00Added an answer on May 30, 2026 at 6:10 pm

    If you want to put the \ character (which is the escape character) inside a string, you’ll need to escape it:

    string = string.replaceAll (" ", "\\ ");
    

    A single \ is a escape sequence leading character, such as with \n (newline) or \r (carriage return). The full list of single-character escapes is:

    \b    backspace
    \t    tab
    \n    linefeed (newline)
    \f    form feed
    \r    carriage return
    \"    double quote
    \'    single quote
    \\    backslash
    

    This is in addition to the octal escape sequences s such as \0, \12 or \377.

    The reason why your separatorChar solution won’t work is because that gives you the separator char (/ under UNIX and its brethren), not the escape character \ that you need.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into

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.