jQuery in Action, 2nd edition*
The moose likes Linux / UNIX and the fly likes Problem running cmd From Java only. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Problem running cmd From Java only." Watch "Problem running cmd From Java only." New topic
Author

Problem running cmd From Java only.

Ravi Kumar
Greenhorn

Joined: Oct 15, 2007
Posts: 16
Hi All,

I have installed a software in a terminal and can be invoked using the following cmd
$ tclsh myinstalledsoftware.tcl inputfile

This has Tcl/Tk dependency, so installed latest version of it in my home directory.
I have set alias for the 'tclsh' in my .bashrc file which points to Tcl/Tk installed in my directory

I can run the same cmd in the terminal with no issues.
I have issues when i try to run the cmd from JAVA.
I'm using the following code to execute



I get the following error.

Tcl initialization error:
Can't find a usable init.tcl in the following directories:
{list of dir}
/usr/share/tcl8.4/init.tcl: version conflict for package "Tcl": have 8.5.8, need exactly 8.4
version conflict for package "Tcl": have 8.5.8, need exactly 8.4
while executing
"package require -exact Tcl 8.4"
(file "/usr/share/tcl8.4/init.tcl" line 19)
invoked from within
"source /usr/share/tcl8.4/init.tcl"
("uplevel" body line 1)
invoked from within
"uplevel #0 [list source $tclfile]"
This probably means that Tcl wasn't installed properly.


Any suggestions are greatly helpful
Thank You!
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Ravi Kumar wrote:
I have set alias for the 'tclsh' in my .bashrc file which points to Tcl/Tk installed in my directory

I can run the same cmd in the terminal with no issues.
I have issues when i try to run the cmd from JAVA.
I'm using the following code to execute




Java will not look into your bashrc-file. A primitiv hack might be: Start your shell from java, to execute the command:


A lot of stuff is to be considered by exec, which you may read up here: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html? but you'r actual problem not, afaik.

Maybe you may specify your settings from java directly, doing your job without bash-involvement. I heard ProcessBuilder is the preferred solution over Runtime.getRuntime, but don't know details/arguments.


http://home.arcor.de/hirnstrom/bewerbung
Ravi Kumar
Greenhorn

Joined: Oct 15, 2007
Posts: 16
Thanks for the quick reply, Stefan.

Nothing worked
I tried,
Process p = Runtime.getRuntime ().exec ("/bin/bash -c 'tclsh myinstalledsoftware.tcl inputfile'");

I tried loading the environment variable using process builder.
I tried executing the alias cmd before the actual cmd.

I have settled down to the least problem which i found, which may not be apt here.
But here is the problem...

I have an alias in my .bashrc file.
alias tclsh='/home/ravi1/tcl/lib/tcl8.5'

So when i execute in the terminal:
$ which tclsh
alias tclsh='/home/ravi1/tcl/bin/tclsh8.5'
~/tcl/bin/tclsh8.5

But when in a script, like the following

#!/bin/bash
which tclsh

The output of the script is :
$ ./runscript
/usr/bin/tclsh

Any suggestions would help...
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Well, just use the absolute path:


Ravi Kumar
Greenhorn

Joined: Oct 15, 2007
Posts: 16
Wow !
Simple but effective. Thanks very much Stefan..
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15661
    
  15

Any time you're running a process indirectly, it's a good idea to use the absolute path of the command, since you don't always know with certainty what the current working directory is. Cron tasks are another case where that's good advice.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem running cmd From Java only.
 
Similar Threads
Announcement: New BETA TclBlend 1.4 precompiled Binary available for Windows.
Classpath problem?
Apache and Tomcat on Mac OS X 10.5?
How to install tomcat5 in Ubuntu 11.04 when it is not in the apt-cache?
java.lang.NoClassDefFoundError: org/apache/tomcat/util/buf/B2CConverter