|
SageTV Studio Discussion related to the SageTV Studio application produced by SageTV. Questions, issues, problems, suggestions, etc. relating to the Studio software application should be posted here. |
|
Thread Tools | Search this Thread | Display Modes |
#1
|
||||
|
||||
String[] and exec() in Java
I'm launching a process in Java (from a Sage plugin) using this command:
Code:
process = Runtime.getRuntime().exec(command, env); Code:
String[] command = {"wine","/opt/sagetv/server/comskip/comskip.exe", "/opt/sagetv/server/comskip/SpongeBob.mpg"}; Code:
List<String> CommandList = new ArrayList<String>(); CommandList.add("wine"); CommandList.add("/opt/sagetv/server/comskip/comskip.exe"); CommandList.add("/opt/sagetv/server/comskip/SpongeBob.mpg"); String[] command = (String[])CommandList.toArray(new String[CommandList.size()]); Code:
for (int j=0; j<CommandList.size(); j++) { Command[j] = CommandList.get(j); }
__________________
Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders. |
#2
|
||||
|
||||
I sorted this one out. I simplified the code in my example above, the FileName parameters to comskip was actually a quoted String, like this:
String FileName = "SpongeBob.mpg"; String QuotedFileName = "\"" + FileName + "\""; and that caused the problem. When I just used CommandList.add(FileName) it worked. So this leads me to another questions - How do I pass a quoted string to comskip? If I do not enclose the string in quotes any FileName that has a white space in it will fail because comskip uses a space as a delimiter.
__________________
Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders. |
#3
|
|||
|
|||
When you call exec() with an array like you are, Java takes care of quoting the args so white spaced args should work fine. You only need to worry about quoting white spaced args if you're using the single String arg version of exec() (i.e. passing the entire command + args as a single string argument to exec()).
__________________
Twitter: @ddb_db Server: Intel i5-4570 Quad Core, 16GB RAM, 1 x 128GB OS SSD (Win7 Pro x64 SP1), 1 x 2TB media drive Capture: 2 x Colossus STB Controller: 1 x USB-UIRT Software:Java 1.7.0_71; SageTV 7.1.9 Clients: 1 x HD300, 2 x HD200, 1 x SageClient, 1 x PlaceShifter Plugins: Too many to list now... |
#4
|
||||
|
||||
OK, but I thought comskip itself needs to have the file name in quotes so it can tell it's one parameter, not two.
So if I pass an array {"wine","comskip","some file name.mpg"} won't comskip see three parameters (some, file, name.mpg) instead of one?
__________________
Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders. |
#5
|
||||
|
||||
It doesn't sound like it. I've never used that method before but it sounds easier. I know when I built the whole string myself I had to escape spaces with backslashes on Linux. Something like this:
Code:
s.replaceAll(" ", "\\ ");
__________________
Server: Intel Core i5 760 Quad, Gigabyte GA-H57M-USB3, 4GB RAM, Gigabyte GeForce 210, 120GB SSD (OS), 1TB SATA, HD HomeRun. Extender: STP-HD300, Harmony 550 Remote, Netgear MCA1001 Ethernet over Coax. SageTV: SageTV Server 7.1.8 on Ubuntu Linux 11.04, SageTV Placeshifter for Mac 6.6.2, SageTV Client 7.0.15 for Windows, Linux Placeshifter 7.1.8 on Server and Client, Java 1.6. Plugins: Jetty, Nielm's Web Server, Mobile Web Interface. |
#6
|
|||
|
|||
Think of it this way: When you pass the command line as an array of strings, Java automatically quotes each element of the array. That's why you had problems when you quoted the string yourself; Java was quoting your quoted string. Hopefully that makes sense?
__________________
Twitter: @ddb_db Server: Intel i5-4570 Quad Core, 16GB RAM, 1 x 128GB OS SSD (Win7 Pro x64 SP1), 1 x 2TB media drive Capture: 2 x Colossus STB Controller: 1 x USB-UIRT Software:Java 1.7.0_71; SageTV 7.1.9 Clients: 1 x HD300, 2 x HD200, 1 x SageClient, 1 x PlaceShifter Plugins: Too many to list now... |
#7
|
||||
|
||||
Yep, it works without doing anything special. I'm pleasantly surprised. Thanks for the help.
Now only this issue to sort out: http://forums.sagetv.com/forums/showthread.php?t=49935
__________________
Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders. |
#8
|
||||
|
||||
Quote:
When you type in a command line manually from the console, you must quote the individual arguments so the shell (not the program being called) knows how to parse them into an array. But when you pass an array of arguments into exec(), there is no such parsing, because the arguments are already in the form that the program expects. So no additional quoting of arguments is needed in that case.
__________________
-- Greg |
#9
|
||||
|
||||
Greg,
Now I understand. Thanks. I was hesitant at first to use exec() with an array instead of one big String, but now I'm glad I did. It's much cleaner and generally avoids problems. It's especially nice that I can pass environment variables as well. That came in handy. Tom
__________________
Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders. |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to shorten a string? | tmiranda | SageTV Studio | 6 | 10-10-2009 09:59 AM |
What is the best way to get the width in pixels of a string? | BobPhoenix | SageTV Studio | 4 | 12-16-2007 02:56 AM |
Studio String help | evilpenguin | SageTV Studio | 1 | 06-10-2006 08:55 PM |
String manipulation in Studio | Alfiegerner | SageTV Studio | 3 | 05-16-2006 05:24 PM |
java.lang.StringIndexOutOfBoundsExeption: String index out of range: 122830 | shibbywowdude | SageTV Software | 4 | 09-27-2005 10:31 PM |