aspose file tools*
The moose likes Linux / UNIX and the fly likes How to call a Linux program from Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "How to call a Linux program from Java?" Watch "How to call a Linux program from Java?" New topic
Author

How to call a Linux program from Java?

alex savu
Greenhorn

Joined: Nov 13, 2008
Posts: 5
Hello everybody!
I need some help with a Java application which must run under Linux (Ubuntu) and this application must at some point (e.g. after pressing a button) run a .o program (in my case "disksim.o") with a number of parameters. Running the program from the terminal would look like this:

./disksim param1 param2 param3..........etc

I need this to be done automatically by a Java program.
Can anyone please give some tips about this issue?

Thank you in advance, all the best!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Alex,

Use java.lang.ProcessBuilder, which is quite straightforward.

Note that a *.o file on UNIX is normally an unlinked object file, and can't be executed as-is; you have to link it as an executable.


[Jess in Action][AskingGoodQuestions]
alex savu
Greenhorn

Joined: Nov 13, 2008
Posts: 5
Hi!
Thanks for the reply. What do you mean by "linking"? What exactly must I do?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

When you have written a program in for example C or C++, there are two steps to make an executable program out of it: compiling and linking. The compiler takes your source code (*.c, *.cpp or something similar) and outputs an object file (*.o). That has to be linked (with a linker) to the necessary libraries into an executable program. So your *.o file is not a complete, runnable executable - you have to link it to get an executable.

Did you write that disksim program yourself, or do you have the source code for it?

GCC (the most used C and C++ compiler on Linux) can do compilation and linking in one step for you, if you call it the right way, for example (assuming you have the source file disksim.c):

gcc disksim.c -o disksim


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
alex savu
Greenhorn

Joined: Nov 13, 2008
Posts: 5
Hello!

DiskSim is a complex storage system simulation environment which can be used only in Linux. When you download it, you download only the sources and then you have to make it with gcc. Then the disksim.o file is genetated and you run it from terminal with ./disksim and some parameters. It reads an input file and generates an output file. Does this mean that it is already linked?

Also, I can only run it from it's own directory and I would like to run it from anywhere in the file system. Do you think is this possible and if it is, how do I do it?

Thanks, all the best!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Not only disksim.o is generated, but also the executable which is called disksim (without any extension) is generated. That's the executable, not disksim.o. If you are typing ./disksim, then you are running the executable in the file "disksim" in the current directory (not "disksim.o"). Note that "." means "current directory". If you want to run it from somewhere else, use the complete path, for example:

/home/alex/somedirectory/disksim

Or add the directory that contains the disksim executable to your PATH.

Forget about the disksim.o file. It's not the executable.
alex savu
Greenhorn

Joined: Nov 13, 2008
Posts: 5
Yup, you're right about the disksim file. Don't know why I confused the executable with the .o file.
Can you please tell me more specifically how to run the file from another folder? I found that to create a new process you use:

Runtime run = Runtime.getRuntime();

Process p = new Process();

String[] cmd = {"/bin/sh", "-c", "./disksim Seagate-Cheetah15k5.parv output.out ascii workload2.trace 0"};

p = run.exec(cmd);


This would be supposing that disksim executable is in the current directory. So to run it from another directory do I use something like this:

String[] cmd = {"/bin/sh", "-c", "/home/alex/somedirectory/./disksim Seagate-Cheetah15k5.parv output.out ascii workload2.trace 0"};

?

If you could also please explain what the first 2 elements mean ("/bin/sh" and "-c"), 'cause I couldn't find anywhere the explanation.

I also found somewhere that in openSUSE (rpm-based) you can copy the executable anywhere and run it from there, or you can even copy it into root/bin folder and than run the program from anywhere in the file system without any constraints. I use Ubuntu which is deb-based and this doesn't work. Or does it and I am missing something?

Thanks everybody,
Cheers!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

alex savu wrote:This would be supposing that disksim executable is in the current directory. So to run it from another directory do I use something like this:

String[] cmd = {"/bin/sh", "-c", "/home/alex/somedirectory/./disksim Seagate-Cheetah15k5.parv output.out ascii workload2.trace 0"};

Yes, except that it is not necessary to leave ./ in there:

String[] cmd = {"/bin/sh", "-c", "/home/alex/somedirectory/disksim Seagate-Cheetah15k5.parv output.out ascii workload2.trace 0"};

And ofcourse for "/home/alex/somedirectory" you must substitute the full name of the directory that the disksim executable is in.

/bin/sh is the standard shell on Unix and -c is an option for sh that says "execute the commands following the -c" (which is in this case the command for running your disksim program).

Whether you can copy the executable anywhere and run it does not depend on whether the system uses RPM, DEB or some other packaging system. It depends on the program itself, and how it expects to find for example configuration files.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

But why should it be called from sh?



http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to call a Linux program from Java?