aspose file tools*
The moose likes Java in General and the fly likes how to excetue a binary file placed onserver which hosted unix Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to excetue a binary file placed onserver which hosted unix" Watch "how to excetue a binary file placed onserver which hosted unix" New topic
Author

how to excetue a binary file placed onserver which hosted unix

kamabotti khan
Greenhorn

Joined: Jul 13, 2010
Posts: 22
Hi

I have a binary file(a file similar to exe file on windows) named msg placed on one of our server which is hosted on unix .The msg is placed in one of the directories in the server location.

I run that msg file ,(which opens a ticket and closes automatically) on unix using a command like this

.\msg and it options and one of the option is a config file that is also placed in the same directory, so now run it something like this

command------------ .\msg -f /tmp/test1.conf and pass required parameters.
So now I have to run this msg (executable file) from java and pass those parameters while running the file, some thing like

if(cond=true)
{
.\msg -f /tmp/test1.conf and pass required parameters
}

how to do that in java ,how can i call that file and excetue that file.Please let me know on how to do this

Thankyou
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hi, and welcome to the Ranch!

If you want to execute a command on a remote linux box, the usual way to do it is with ssh. Something like this:


This is how you do it from the command line, and has nothing to do with Java.

If you want to do this from Java, you have two choices:

1) Use ProcessBuilder or Runtime.exec to execute that ssh command as an external process. You may run into difficulty with the password here.

2) I think there's a 3rd party ssh client library available, so you could google for that.
kamabotti khan
Greenhorn

Joined: Jul 13, 2010
Posts: 22
I am executing the command already on a linux box like this

.\msg -f /tmp/test1.conf and pass required parameters.

but now this command should be exceuted automatically from java after having one of the condition fulfiled in my application.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

kamabotti khan wrote:I am executing the command already on a linux box like this

.\msg -f /tmp/test1.conf and pass required parameters.

but now this command should be exceuted automatically from java after having one of the condition fulfiled in my application.


So use ProcessBuilder or Runtime.exec.

It's really not clear what your requirements are or what difficulty you're having.
kamabotti khan
Greenhorn

Joined: Jul 13, 2010
Posts: 22
Hi I have a executable file mesg on /tmp location on one our dev server which is hosted on a unix box which again takes a test.conf as a parameter ,It creates a ticket and close it.

So I manually run it logging into the server like this

./mesg -f /tmp/autoticket.conf source=Event severity=MINOR appname='Hai' autoticket=YES appcomponent=`/usr/bin/perl -e"print time();"` prisev=P2 hostname=xyz='GET'' msg=' This is for auditing' origin=XYZ

Now I have to automate this process using java I have to call this executable and pass the parameters.


I have two options I can run the file which is located in host or I can make that conf file and executable file as part of my build and make it run on the dev server when i deploy my code.


I did this I copied the mesg executable file and also test.conf in webinf /classes folder and then I executed this command in my file ticket.java in xyz package

Process p = Runtime.getRuntime().exec("./mesg -f test.conf source=Event severity=MINOR appname='HAI' autoticket=YES appcomponent="+DateFormat.getInstance().format(new Date())+ " "+"prisev=P2 "+ " " + "hostname= " +POSTEMESG_URL+ " " + " This is for auditing origin=" XYZ");

But I get an IOException error ./mesg file not found exception. Please suggest why it is not finding the file

So am doing it right Should I use the other option of not making it part of build and just runing it from the /tmp location of server.


Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 581
    
    7

i don't really understand your requirements, but....

if you want to kick off a process (program) every (fill in period) then you should use cron

if you want to interact with the OS use a language like python.

otherwise provide us with your full set of requirements

-steve
kamabotti khan
Greenhorn

Joined: Jul 13, 2010
Posts: 22
hi,

The requirement is I have to call an executable file postemsg which is an IBM product this should be called from my java file after a one condition is fulfilled.

I have two versions of it a windows executable file and a linux executable file since my code is deployed on server which is hosted on unix box ,I have to use the linux one.

postemsg

Posts an event to the event server using non-Tivoli communication.

Syntax

postemsg -S server | -f configuration_file [-m message] [-r severity] [attribute=value...] class source

Description

The postemsg command sends an event to an event server. This command does not require the use of Tivoli Management Framework services and works in environments where Tivoli software is not installed.

Notes:
Although this command is still supported, the postzmsg command is preferred for sending events using non-Tivoli communication.
If the Tivoli Management Framework is installed on your system, you can use the wpostzmsg (managed node version) or wpostzmsg (endpoint version) command.
Because cache files cannot be shared by applications, you should not use a configuration file for this command that is being used by another program (such as an adapter).
Because this command does not have access to the message catalogs for the language support packs, postemsg displays error messages in English only.

Authorization

None

Options

attribute=value
Assigns a value to any valid attribute. The attribute should be one defined for the event class. Separate multiple attribute=value expressions with spaces.
class
Specifies the class of the event. It must match a class that is configured at the server. Classes are defined by the adapter and listed in the adapter BAROC file. If any blank spaces are in the class name, enclose the class name in double quotation marks.
source
Specifies the source of the event. If any blank spaces are in the source name, enclose the source name in double quotation marks.
-f configuration_file
Specifies the name of the adapter configuration file. You must specify either the -S argument or the -f argument. See the IBM Tivoli Enterprise Console Event Integration Facility Reference for more information about configuration files.
-m message
Specifies the text of the event, enclosed in double quotation marks.
-r severity
Specifies a severity. The severity must be one defined for the event class.
-S server
Specifies the host name or the IP address of the server. Use this option only if portmapper is running on the event server. You must specify either the -S argument or the -f argument.
Examples

The following example for UNIX environments sends a test message that displays an Su_Failure event on the event consoles:

postemsg -S tecbox -r WARNING -m "su login failure." Su_Failure LOGFILE

Now this file is on a tmp directory on server location .
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

After all that, I still don't see what you're having problems with. I already gave you two different kinds of solutions based on whether the command is on a remote or local host. (For some reason I initially thought you had to run the command remotely, but looking back at the thread now, I'm not sure why I thought that.)

So, to repeat what I already said...

If it's local:
Use ProcessBuilder (preferred) or Runtime.exec (which just calls PB anyway). Did you try that? What problems did you encounter.

If it's remote:
Use ProcessBuilder to execute ssh (which may not work because I don't know if ssh will take a password from a non-tty, but that's not a question for this forum).
OR
Use a third-party Java ssh library, which you should be able to find with a quick Google search.

So, what have you tried so far and what difficulties are you encountering?
kamabotti khan
Greenhorn

Joined: Jul 13, 2010
Posts: 22
Hi this part of the code I tried this



I placed the postemsg executable and test.conf in the WEB_INF folder and which also gets placed in WEB-INF/classes folder after the build ,(using an ANT Script)

Now I get an IO exception ./postemsg file not found. and also one more thing if it is not able to find the postemsg file then it should say "postemsg" file not found,but why it is adding the ./ the unix command used to run files before it
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

1) When posting code, please UseCodeTags.(⇐click) I have edited your post to add them. Don't make it blue, or for that matter, use excessive color anywhere.

2) One line was far too long and ended up leading to having to scroll right. I broke that line into two pieces. In the future, please keep your code lines shorter.

As for your actual problem, when you call Runtime.exec(), if you give it a relative path, it must be relative to the current working directory of the JVM executable. Since we don't usually know what that is and can't usually control it--especially in the case of a web app--it's better to use an absolute path.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6


why it is adding the ./ the unix command used to run files before it


When you refer to path X, if it is not an absolute path, then in most contexts, it is, by definition, ./X

It's the same thing. "X" means "X relative to the current directory."

kamabotti khan
Greenhorn

Joined: Jul 13, 2010
Posts: 22
I tried to give the absolute path in different ways

No 1:


This Gives the error

Error: java.io.IOException: Cannot run program "./C:/Desktop/workspaces/default/MyProject/WebContent/WEB-INF/postemsg": java.io.IOException: error=2, No such file or directory



No 2: I even tried with windows executable


This Gives the error

Error: java.io.IOException: Cannot run program "C:/Desktop/workspaces/default/MyProject/WebContent/WEB-INF/postemsg.exe": java.io.IOException: error=2, No such file or directory


No 3 :


This gives the error as

Error: java.io.IOException: Cannot run program "./java.io.FileInputStream@14654146": java.io.IOException: error=2, No such file or directory

Please Help me on this ,what am I doing wrong
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

kamabotti khan wrote:
Please Help me on this ,what am I doing wrong


You don't need us to tell you. The error messages already told you. You're trying to execute programs that don't exist, such as C:/Desktop/workspaces/default/MyProject/WebContent/WEB-INF/postemsg.exe. (Or possibly they exist but you don't have the permissions needed to see, read, or execute them.)

Please don't use so much red. It's painful to read.

kamabotti khan
Greenhorn

Joined: Jul 13, 2010
Posts: 22
I apologize about the colors ,but the file exsists at the location and also has read write permissions.

but when I specify the no :3 method it was looking for the file on the server location where I deployed the war .I even placed the file there
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

kamabotti khan wrote:I apologize about the colors ,but the file exsists at the location and also has read write permissions.


You can either question that assumption, or you can conclude that Java is arbitrarily lying to you and give up. Those are the only two options I see.

Have you tried executing this at a command prompt? (Copy and paste directly from here, don't type it in.)


Or this?


Again, copy paste directly from here, which I got directly from the error message you (presumably) pasted in here.
kamabotti khan
Greenhorn

Joined: Jul 13, 2010
Posts: 22
I tried to execute the following using the command line

It displayed the usage of running the command ,so it was able to find the file, But I am deploying my war on the dev server which is hosted on a unix box ,and launching the app or running the app there will that be a problem of not locating the file
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

You usually cannot execute Windows executables (.exe) on Unix. You may be able to get it to work using Wine, but if that doesn't work then you need to create or find a Unix equivalent of the executable.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859



pops up the Windows calculator on my desktop.

I don't think you need the ./ in front of C: either.

It's either a path issue, or even if the executable does exist, could be relative.

Oh, and don't try executing ./java.io.FileInputStream@14654146 !!

Also exec has a few signatures, one of which is:
exec (String command, String[] envp, File dir)

Executes the specified string command in a separate process with the specified environment and working directory.
This is a convenience method. An invocation of the form exec(command, envp, dir) behaves in exactly the same way as the invocation exec(cmdarray, envp, dir), where cmdarray is an array of all the tokens in command.

More precisely, the command string is broken into tokens using a StringTokenizer created by the call new StringTokenizer(command) with no further modification of the character categories. The tokens produced by the tokenizer are then placed in the new string array cmdarray, in the same order.

Parameters:
command a specified system command.
envp array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.
dir the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process.
Returns:
A new Process object for managing the subprocess
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

William P O'Sullivan wrote:
I don't think you need the ./ in front of C: either.


Indeed. That will almost certainly not work on either platform. It says "look at the C drive below the current directory" or "look in the directory name 'C:' below the current directory".

It's either a path issue, or even if the executable does exist, could be relative.


It's still not totally clear to me what the OP is trying to accomplish or what his setup is, but it looks like it's a path issue and a problem with trying to execute a binary on the wrong platform.
kamabotti khan
Greenhorn

Joined: Jul 13, 2010
Posts: 22
The problem was with the path,I gave the path of the files located on the server and it worked ,I don't get any IOException ,but now it is not getting executed (no results) ,so to check the syntax of the string is being passed correctly,I wanted to print p variable but it gives me output
java.lang.UNIXProcess@e5c71af instead of the string



I get an output like P:::java.lang.UNIXProcess@e5c71af instead of this

P:::/local/tmp/postemsg -f /local/tmp/postemsg/test.conf source=Event severity=MINOR appname='HAI' autoticket=YES prisev=P2 hostname=xyz This is for auditing origin=xyz.

So how can I print p var so that I can see how the whole string is being passed


 
 
subject: how to excetue a binary file placed onserver which hosted unix