exec and bash

H

Hakim

hi ng,

what i wanna do is to exec a bash-command from the java code. this
command should create an archiv. when i print the command w/
System.out.println it does what i want, so the bash syntax is correct.
there is no exception.
String[]


names={"projects","pics","protocols","changes","conditions","customer_to_project"};



for (int i=1;i<names.length-1;i++){

try{

String[] command ={"/bin/bash",

"/bin/tar cj

/usr/local/tomcat/webapps/weghaus/documents/"+names+"/"+projectId+">/archiv/"+projectId+"_"+names+".tar.bz2"};

Process child =

Runtime.getRuntime().exec(command);



catch(IOException e){

e.printstackTrace();

}

could somebody help me to fix it?

tia
H.Bada
 
A

Andrew Thompson


Don't you mean 'Hi NGs'?

Please cross-post in future.
<http://www.physci.org/codes/javafaq.jsp#xpost>

....
"/bin/tar cj

/usr/local/tomcat/webapps/weghaus/documents/"+names+"/"+projectId+">/archiv/"+projectId+"_"+names+".tar.bz2"};


Note that wide code samples usually wrap and break,
try to keep them thinner..

Lastly, since you seem to be new to this and are posting
from one of the larger web interfaces to usenet, please read this..
<http://www.physci.org/codes/javafaq.jsp#usenet>
 
G

Gordon Beaton

what i wanna do is to exec a bash-command from the java code. this
command should create an archiv. when i print the command w/
System.out.println it does what i want, so the bash syntax is
correct. there is no exception.

First, don't multipost. Followups to c.l.j.programmer.

Your bash syntax is wrong. The command must be organized like this:

command[0]: bash
command[1]: -c
command[2]: complete command line and arguments

As far as I can tell, you are missing the -c.

Note that there is absolutely no reason to use bash for this. You can
run tar directly and specify the output file with 'f':

command[0]: tar
command[1]: cjf
command[2]: "/archive/" + projectId + "_" + names + ".tar.bz2"
command[3]: filename
command[4]: filename...

(etc)

/gordon
 
R

Roedy Green

String[] command ={"/bin/bash",

"/bin/tar cj

/usr/local/tomcat/webapps/weghaus/documents/"+names+"/"+projectId+">/archiv/"+projectId+"_"+names+".tar.bz2"};



You could have coded that like this:

// command processor program parm parm
Process p = exec ( new String[]{
"/bin/bash",
"/bin/tar",
"cj",
"/usr/local/tomcat/webapps/weghaus/documents/"+names+"/"+projectId
});

InputStream is = p.getInputStream();

Now it is your duty to read is, and log it to
"/archiv/"+projectId+"_"+names+".tar.bz2"

If that seems like to much work, see what happens when you put ">" in
a command parameter all by itself.

If that does not work, see what happens when you pass the entire
command as a single string.
 
R

Roedy Green

String[] command ={"/bin/bash",

"/bin/tar cj

/usr/local/tomcat/webapps/weghaus/documents/"+names+"/"+projectId+">/archiv/"+projectId+"_"+names+".tar.bz2"};

Process child =

Runtime.getRuntime().exec(command);


Start with something simple and gradually add complexity.

See http://mindprod.com/jgloss/exec.html

// Linux Bash
String[] cmd = {"/bin/bash", "-c", "rm /dirA/*"};
Runtime.getRuntime().exec( cmd );

you only need bash for internal commands, ones that don't have a
corresponding standalone exe file, e.g. echo, pwd, cd, cp, rm, ls,
mkdir.

Nearly evenone fails to use a command processor when one is needed for
exec. You are the first person I have ever seen to do the reverse.
 
G

Gordon Beaton

you only need bash for internal commands, ones that don't have a
corresponding standalone exe file

You also need a shell when you want any shell features like
redirection or filename globbing.

These are not "exe" files on Unix or Linux, which do not use DOS style
file extensions like "exe" and do not use the "exe" file format.
e.g. echo, pwd, cd, cp, rm, ls, mkdir.

Of those, only cd needs the shell. The other examples are all "normal"
programs (although though many shells, including bash, may have
built-ins for "echo" and "pwd").

/gordon
 
R

Raymond DeCampo

Roedy said:
String[] command ={"/bin/bash",

"/bin/tar cj

/usr/local/tomcat/webapps/weghaus/documents/"+names+"/"+projectId+">/archiv/"+projectId+"_"+names+".tar.bz2"};




You could have coded that like this:

// command processor program parm parm
Process p = exec ( new String[]{
"/bin/bash",
"/bin/tar",
"cj",
"/usr/local/tomcat/webapps/weghaus/documents/"+names+"/"+projectId
});

InputStream is = p.getInputStream();

Now it is your duty to read is, and log it to
"/archiv/"+projectId+"_"+names+".tar.bz2"

If that seems like to much work, see what happens when you put ">" in
a command parameter all by itself.

If that does not work, see what happens when you pass the entire
command as a single string.


Or you could sit down and read some documentation until you understand
how a) Runtime.exec(), b) the bash shell and c) UNIX/Linux processes
work. Then it will be patently obvious what to do.

Unless you'd rather just hack around until you stumble upon something
that works today and may break tomorrow or on another machine.

HTH,
Rau
 
R

Roland

How do you find out which commands are internal?
RTFM
$man csh

In bash, you can use the 'type' command:
$ type ls
ls is /usr/bin/ls
$ type pwd
pwd is a shell builtin
$ type type
type is a shell builtin
I gather on Linux
there is no .exe extension, but there is an executable attribute on
the file. Does Linux have something the equivalent of associations --
to somehow associate a file with a program to execute it?

Magic numbers
<http://en.wikipedia.org/wiki/File_format#By_.22magic_number.22>
Shebang
Is there
some master table of file signatures to sort this out?
/usr/share/magic (may be located elsewhere)

The 'file' command can be used to display the type of a file. It uses
magic numbers (amongst others) to determine the file type), e.g.
$ file mymusic/Torn.mp3
mymusic/Torn.mp3: MP3 file with ID3 version 2.3.0 tag
$ file bin/panoramix
bin/panoramix: Bourne-Again shell script text executable
--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
 
R

Raymond DeCampo

Roedy said:
your chastisement reminds me of a story Dr. Melzak, my combinatorics,
math prof once told.

A prof was writing out a long proof on the blackboard in front of the
class. A student popped up his hand and said, "I don't see how you
got from step 14 to step 15".

"It's trivial" the prof retorted.

"I don't see it. Could you please explain." the student replied.

The prof opened his mouth, then closed it again. He stared at the
blackboard. He left the room, and just before the end of the class,
he returned. He announced, "I knew it. Just as I thought. It IS
trivial!" of course, with no further explanation.

Usually this story is told with phrase "intuitively obvious" in the
place of trivial.
The answer to Hakim's question is trivial ONCE you correctly
understand how exec works. You will never figure that out from reading
documentation alone, unless perhaps you are a natural lawyer. You
need to do some experimenting as well to verify or dispel your various
assumptions. Only then does the whole thing become too simple for
words.

It is cruel to chastise someone for not already understanding
something. You too were in that same state at one point. It is a cheap
way to gain self esteem at the expense of others. There are trillions
of things to learn and all of us have just begun.

You've misread my post. The only person I was chastising was you for
encouraging experimentation without understanding. That is the
definition of hacking. It is frustrating to me to see this kind of
advice given out.

I was not trying to get a "self-esteem" boost from pointing out the OP
does not understand something. I was encouraging the OP to go out and
try to understand it.

I disagree with your assessment that a "natural lawyer" like ability is
required to come up with the correct way to execute an external process
in Java. A person need only understand the bash shell well enough
construct a command line and Runtime.exec() well enough to understand
how to invoke bash with that command line. Further subtleties like
consuming the process streams will likely require more prodding.

There is nothing wrong with using experimentation to encourage further
understanding. However, encouraging someone to simply experiment with
their code until something works is not helpful.

Ray
 
T

Thomas Weidenfeller

Roedy said:
How do you find out which commands are internal?

I assume you mean build-in into a shell.
Most build-ins in a shell are there for performance reasons, and also do
exist as a stand-alone program. When the shell has a command as a
build-in it does not have to put the OS through the effort to start the
command as an external process (fork/exec), which gives considerable
speed gains.

The final say about what is build into a shell has a particular shell's
documentation or the source code, since the list varies from shell to shell.
I gather on Linux there is no .exe extension, but there is an executable attribute on
the file. Does Linux have something the equivalent of associations --
to somehow associate a file with a program to execute it?

Unfortunately, there is more than one such mechanism in Unix/Linux.
There is a basic one used to identify a file type, common to all Unix
versions, plus each desktop brings its own mechanism for associating
file types with actions. You have to check your desktop documentation to
figure out how your desktop does things.

The 'file' program is the program which can identify the type of a file
(sometimes it just guesses). You find it on all Unix systems, but it
doesn't associate any actions with a file type.
Is there
some master table of file signatures to sort this out?

/etc/magic for 'file', and each desktop has additional files.

/Thomas
 
R

Roedy Green

You've misread my post. The only person I was chastising was you for
encouraging experimentation without understanding. That is the
definition of hacking. It is frustrating to me to see this kind of
advice given out.

I went through the equivalent troubles trying to understand why exec
did not behave as I expected in Windows. The docs were no help. Java
docs rarely give you any help with platform-specific problems.

I figured it out by experiments.

In general I find documentation one of the least useful sources of
information. Sample code is one of the best. The people who write
documentation do so reluctantly. They are not good at it. They
primarily want to write code.

I often write authors pointing out ambiguous statements in their
documentation and suggesting clearer wordings. They never get it. They
write back explaining that if you look at it this way, the meaning is
clear.


I wrote an essay on how documentation should work. See
http://mindprod.com/jgloss/author.html. Part of the problem is
usually you are overwhelmed with detail that is too obvious or too
advanced. You need to be able to extract the information for your
current level.
 
R

Raymond DeCampo

Roedy said:
I went through the equivalent troubles trying to understand why exec
did not behave as I expected in Windows. The docs were no help. Java
docs rarely give you any help with platform-specific problems.

Which is why I suggested that the OP understand how bash and UNIX/Linux
processes work first.
I figured it out by experiments.

In general I find documentation one of the least useful sources of
information.

I've noticed. An interesting attitude for a guy who maintains a bunch
of documentation himself.
Sample code is one of the best. The people who write
documentation do so reluctantly. They are not good at it. They
primarily want to write code.

I often write authors pointing out ambiguous statements in their
documentation and suggesting clearer wordings. They never get it. They
write back explaining that if you look at it this way, the meaning is
clear.


I wrote an essay on how documentation should work. See
http://mindprod.com/jgloss/author.html. Part of the problem is
usually you are overwhelmed with detail that is too obvious or too
advanced. You need to be able to extract the information for your
current level.

Ray
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top