SageTV Community  

Go Back   SageTV Community > SageTV Development and Customizations > SageTV Studio
Forum Rules FAQs Community Downloads Today's Posts Search

Notices

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.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 07-31-2010, 03:17 AM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
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);
If I setup command like this it works:

Code:
String[] command = {"wine","/opt/sagetv/server/comskip/comskip.exe", "/opt/sagetv/server/comskip/SpongeBob.mpg"};
If I do this it fails: (It launches wine which in turn launches comskip but then comskip errors out saying the file supplied is not an MPEG.)

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()]);
What am I doing wrong? I tried different variations converting the List to an array including a for loop like this:

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.
Reply With Quote
  #2  
Old 07-31-2010, 04:03 AM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
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.
Reply With Quote
  #3  
Old 07-31-2010, 08:04 AM
Slugger Slugger is offline
SageTVaholic
 
Join Date: Mar 2007
Location: Kingston, ON
Posts: 4,008
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...
Reply With Quote
  #4  
Old 07-31-2010, 08:49 AM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
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.
Reply With Quote
  #5  
Old 07-31-2010, 09:20 AM
jreichen's Avatar
jreichen jreichen is offline
Sage Icon
 
Join Date: Jul 2004
Posts: 1,192
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(" ", "\\ ");
Two backslashes are required because they're in a Java literal string and it will escape them too.
__________________
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.

Reply With Quote
  #6  
Old 07-31-2010, 09:57 AM
Slugger Slugger is offline
SageTVaholic
 
Join Date: Mar 2007
Location: Kingston, ON
Posts: 4,008
Quote:
Originally Posted by tmiranda View Post
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?
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...
Reply With Quote
  #7  
Old 07-31-2010, 10:02 AM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
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.
Reply With Quote
  #8  
Old 07-31-2010, 10:08 AM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
Quote:
Originally Posted by tmiranda View Post
OK, but I thought comskip itself needs to have the file name in quotes so it can tell it's one parameter, not two.
The part you're missing is that command-line programs don't receive their arguments as one big unbroken string representing the entire command line. They receive them as an array of individual argument strings.

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
Reply With Quote
  #9  
Old 07-31-2010, 10:33 AM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
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.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT -6. The time now is 05:32 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.
Copyright 2003-2005 SageTV, LLC. All rights reserved.