1

I have a Java bot that I'm working on which takes input from a user via an IRC message. The user types in something like .host file=foo file=bar hostname="this is a name" and it will start a server for them based on their input.

Right now I have a method that basically parses their message, looking for hostname="" (must be encapsulated by quotes), splitting the hostname= part, and setting the hostname to whatever their result is (for example, hostname="this is a name" would become -hostname 'this is a name'). I am using regex to do this.

I then split the string into an array (separated by spaces) and pass the information to processbuilder, which executes the command in it's own thread. The final command looks something like this:

./server -hostname "this is a name"

A problem I am running into is that since I am splitting by spaces, if the user adds a dash in the hostname (for example, hostname="this is - a name", it'll think that the dash is referring to an argument, and will basically chop off the rest.

Further explanation: processbuilder will split by spaces, so it will pass "-hostname" , "this" , "is" , "-" , "a" , "name". This is where I'm having trouble, since - means I should be passing an argument, but that's not what I am using it for.

What would be the most efficient way to implement this so that any character being passed is only literally this character? Should I not be splitting at spaces? If I run ./server -hostname "this is - a name" from the linux shell, it will run fine.

I appreciate any and all help. Thank you!

3
  • A project that tries to make Runtime.exec less painful. Not sure it's the best way, but reading the source gives you a good idea how to interact with the Runtime.exec: github.com/RasmanTuta/exec-utils/tree/master/exec-utils/src/… Commented Oct 12, 2012 at 20:06
  • Virtually a duplicate: stackoverflow.com/questions/4916918/… Commented Oct 12, 2012 at 20:12
  • I think they're pretty different. I'm already splitting the string into an array via the suggestion by Mikel. The problem I have is that since the command is dynamic, making sure that - is not interpreted as me trying to pass an argument, but rather literally the character '-'. Commented Oct 12, 2012 at 20:17

2 Answers 2

3

Both Runtime.exec and ProcessBuilder will take a list of commands.

Runtime.exec(new String[]{"./server", "-hostname", "this is - a name"};
ProcessBuilder pb = new ProcessBuilder("./server", "-hostname", "this is - a name");

This basically means that each element is passed to the command (the first argument) as separate parameters, such that you don't need to worry about things like spaces and extra characters.

Sign up to request clarification or add additional context in comments.

Comments

0

I decided to completely change my code and use regex to parse input from the user, instead of the previous method and splitting at spaces. This means that I can simply add the input to an Arraylist as a full "+sv_hostname="test - name"" instead of having "+sv_hostname", "test", "-", "name".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.