• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Execute Linux Command and Return Output

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all!  I am working on a project that has to execute commands on the Linux terminal and I would like Java to handle the logic but I am having a bit of difficulty.

I found the code below online and modified it a little to meet my needs.  When it executes, I receive no output.  But when I run something like "ifconfig" or "echo HelloWorld", I get the expected output.  Also, I tested the command on the terminal and it does work.

What is the difference?  Why does it work when running other commands, but this one specifically fails to return anything?

 
Marshal
Posts: 5675
333
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Must you use Java for this? If not then a shell script or an expect script might be better suited.
 
Tim Cooke
Marshal
Posts: 5675
333
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Alexander Garcia
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would prefer Java as this will be distributed to quite a few people and I am hoping that it will function on Windows and Linux.  This is just a problem I am trying to solve on the Linux side.  Ultimately I am trying to copy a file from a Samba share to the local machine that is running the Java code but have had issues with that as well.  So if I can run terminal commands to determine if the share is already mounted and then copy the file from the file system it should work (it worked when doing these things manually and then programmatically copying the files).  But if I can't get the output from the terminal command I am dead in the water.
 
Tim Cooke
Marshal
Posts: 5675
333
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps using Java's File I/O libraries might be a nicer way to achieve it since a command string as you are attempting to use might not translate very well across platforms or even across different versions of the command you are using. Even path separators vary between platforms, "/" on Linux and "\" on Windows.

Lots of help and info in the tutorials https://docs.oracle.com/javase/tutorial/essential/io/fileio.html
 
Sheriff
Posts: 4644
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be able to just check the exit value from grep to determine if a match was found.

Something like this:

Of course this will not work on Windows.
 
Sheriff
Posts: 28328
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a Java implementation of SMB which I used extensively to copy files around a WAN consisting of a variety of systems. I don't recall it being an issue if shares weren't mounted and it's been several years since I did that, so I believe the shares were created within the Java code. I do remember having to provide the code with passwords for SMB addresses. It was open source so you should still be able to track it down.
 
Alexander Garcia
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron McLeod that worked beautifully!  The people I plan on distributing this to will most likely be using a Raspberry Pi to run the software as it is a cheap system.  Thank you for the assist!
 
Saloon Keeper
Posts: 28320
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:Even path separators vary between platforms, "/" on Linux and "\" on Windows.



However, it is a standard that "/" works as a path separator for Java in ALL operating systems, so that's not a problem.

Incidentally, in Linux, an SMB shared file path is in the form "//hostname/sharename/dirname/dirname/filename". Even outside of Java, since backslashes are perilous to Unix-style users.

Yes, there is a Java library for working with CIFS file sharing.

And no, I don't recommend using Runtime.exec() to execute a series of commands and especially not with redirects. Command-line Redirects and piping are a shell function, and I'm pretty sure that Runtime.exec does not spawn a shell automatically. For one thing, there's only about 8 different shells available to Linux users, so you'd have to indicate which one to use. Put the commands into a shell script and exec() something like "/usr/bin/sh /absolute/path/to/my/script".

Finally, to capture stdout/stderr or set stdin on Runtime.exec() is a lot messier, if I recall, than in that example. You basically have to pre-allocate Java Streams and feed them in as part of the exec(). Then you have to have code to do something with what comes out of (or goes into) the streams.

And welcome to the Ranch, Alexander!
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexander Garcia wrote:What is the difference?  Why does it work when running other commands, but this one specifically fails to return anything?


Because what you have is three separate commands (grep, echo, echo) that the shell (Bash, etc.) chains together. Java's Process can only execute one single command. Ron's solution works because it removes two of the commands. Tim Cooke's suggestion also would have worked - use a script. Because that would be a single command again to Process.
 
Rob Spoor
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:You should be able to just check the exit value from grep to determine if a match was found.

Something like this:

Of course this will not work on Windows.


There are some issues with that. The first is that exitValue will throw an IllegalThreadStateException if the process hasn't finished yet. waitFor can be used to handle that issue.
The other issues all have to do with handling the process' output. When working with Process, you must process the output. Otherwise you run the risk of deadlock. See When Runtime.exec() won't. That's an old article, but it's still relevant.

In this case the solution is two-fold: redirect the error stream so there's only one stream to read from, and then read from that stream (or discard its contents).
 
I don't even know how to spell CIA. But this tiny ad does:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic