Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call UNIX commands from JAVA

 
Dmitriy Bar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I execute UNIX commands from JAVA program running under Windows?
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to run them on the local machine then no, not without some kind of UNIX emulator / shell. Does such a thing exist for windows?
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim Baker:
If you want to run them on the local machine then no, not without some kind of UNIX emulator / shell. Does such a thing exist for windows?

Yes - there is Cygwin - http://www.cygwin.com/ - I've never tried calling any of its commands from Java, but Dmitriy could give it a try.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dmitriy Bar:
Can I execute UNIX commands from JAVA program running under Windows?

This doesn't make sense. The only time I can think this would be a good idea is if you are testing for deployment and don't have access to a Linux machine. Of course then I would question why you are developing for Linux but don't have access for a linux machine.
So what's the story morning glory?
 
Dmitriy Bar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
what I meant was:
you have some kind of AWT or Swing GUI running on your local PC and you want to execute some commands on UNIX server(without openning some kind of UNIX emulator and connecting to the UNIX server) and have result from this execution back to your Java application.
OR maybe there is a way of executing Java application(AWT/Swing) under UNIX OS without openning X Windows etc.
Thanks.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dmitriy Bar wrote:

Hi.
What I meant was:
you run some kind of AWT or Swing GUI on your local PC and want to bring a result of running some commands on UNIX server back to the Java application
(without openning some UNIX emulators and connecting to UNIX manually).
Or how to run AWT or Swing application under UNIX OS without openning X Windows etc.?
Thanks.

If you are wanting to return results back from running a Unix command on a Unix machine but your code resides on a Windows machine, you are going to have to use RMI to invoke methods on the remote machine to execute the commands you are requiring.
You cannot run GUI applications in Unix without the X Windows System running. The X11 system is what allows widgets to be drawn on the screen. Without it, it is pure text. Unless you can write a nice little ASCII GUI in Java.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg wrote: If you are wanting to return results back from running a Unix command on a Unix machine but your code resides on a Windows machine, you are going to have to use RMI to invoke methods on the remote machine to execute the commands you are requiring.
Or any other protocol, really: HTTP/CGI, telnet, ssh, rsh, rlogin, web services, etc. etc. The important thing is that you need to send your command(s) to the remote machine, let it do its stuff, and return the result to you.
You cannot run GUI applications in Unix without the X Windows System running. The X11 system is what allows widgets to be drawn on the screen. Without it, it is pure text. Unless you can write a nice little ASCII GUI in Java.
http://www.pitman.co.za/projects/charva/
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frank Carver:
http://www.pitman.co.za/projects/charva/

That is cool. I actually could find a use for that. Thanks Frank!! Not to mention it is a good solution for this guys problem.
 
Dmitriy Bar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Frank.
Did you try PJAToolkit?
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have recently used the SSH tools from http://www.3sp.com in a big project and it works fabulously. If you have SSH access to the Unix box, it is a great solution - free and secure. There are some example java classes that come with it that show you how to do basic stuff like connect via SSH/SFTP with name/pass and with keys.
Brian
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic