Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Launching processes from Java Cocoa

Launching processes from Java Cocoa
Thread Tools
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Jan 1, 2001, 10:56 AM
 
I'm working on a project that requires root permissions to change a configuration file. I seem to have hit a wall, and was wondering if anyone could give me some pointers.

I intend to make changes to the config file by su'ing to root from within my app and using sed and awk to make the changes.

I figured out how to authenticate users after looking at Gavin's code, reading the ObjC docs for NIInterface, and poking around in JavaBrowser.
This knowledge does me absolutely no good if I can't launch external processes.

Ideas? I figured as a worse case scenario, I could write a class in ObjC that uses NSTask to do what I need, and work with it across the bridge. This seems to be a suboptimal solution though. I'm also not sure how healthy using two different thread models would be.

If anyone is wondering what all this nonsense is for, I'm trying to write a GUI config program for The Internet Junkbuster ( http://www.junkbuster.com ) and need write access to the config file and blocklist.

Thanks in advance
     
Mac Elite
Join Date: Oct 2000
Location: Seattle
Status: Offline
Reply With Quote
Jan 2, 2001, 12:14 AM
 

have a look at the source for nicer - it's in java
http://www.versiontracker.com/moreinfo.fcgi?id=9473
You can take the dude out of So Cal, but you can't take the dude outta the dude, dude!
     
int69h  (op)
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Jan 2, 2001, 01:23 PM
 
Thank you once again Gavin.

I've been quite disgusted with the Java interfaces to Cocoa in the past few days. I think I'm going back to Objective C. Maybe I'll try Java again when the documentation gets a little better. Come to find out the answer to my question was really, really simple.

Code:
/* Code taken from Nicer by Ryan Stevens */ Process p = Runtime.getRuntime().exec(niceit+nval+" "+pid);
Terry
     
Dedicated MacNNer
Join Date: Oct 2000
Location: Pasadena, CA, USA
Status: Offline
Reply With Quote
Jan 3, 2001, 09:21 AM
 
> /* Code taken from Nicer by Ryan Stevens */
> Process p = Runtime.getRuntime().exec(niceit+nval+" "+pid);

Though there are reasons to be annoyed with the Java2ObjC bridge (I've had my share of problems with the bridge) but this little snippet of code shouldn't be one of them - it's pure Java. It'll even work under OS 9 for some commands.


     
int69h  (op)
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Jan 3, 2001, 02:04 PM
 
I got a crash course in Runtime today. After looking at Ryan's code for Nicer, I found some things I wanted to change. I learned quite a bit while modifying the code to launch "/usr/bin/su root -c somecommand". Thanks for the comments people. I think I've got it figured out.
     
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status: Offline
Reply With Quote
Jan 5, 2001, 03:26 PM
 
Before you get real annoyed with Runtime.exec() - which is certainly a possibility - you may want to read this article in JavaWorld which details some of the irregularities with the API. E.G., external processes hanging because of the stdin/out/err buffers, command parsing, etc.
http://www.javaworld.com/javaworld/j...229-traps.html

------------------
     
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status: Offline
Reply With Quote
Jan 5, 2001, 03:29 PM
 
One more thing, it may be easier for you to create a shell script and then use Runtime.exec("sh " + path_to_shell_script) and do all the processing in the more comfortable (and more testable) shell environment.

------------------
     
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Jan 15, 2001, 08:41 PM
 
I've tried using this method before and even though it appeared to compile and execute - nothing happened as was supposed to. Maybe not too reliable.

NSTask seems overcomplex so I never tried it out.

------------------
Think Different.
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:23 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2