LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2003, 12:42 PM   #1
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Rep: Reputation: 16
Question 2 Questions: java calling system commands? PERL vs Java?


1. Can you call system commands from a java program? If so, how?

For example, I want to write a simple program that tells me the time or date or even untar a file. It just never occured to me of doing that stuff before on Java.

I know you can create that on a simple shell script...I'm just exploring new ways of doing stuff.

2. I'm between learning PERL or furthering in Java. My main turn off with Java is speed and the "classes". It's like you are creating small little programs inside your big program. It's a hassle.

This is what I want to do:
a) I want to have the speed of shell scripting for both executing and writing code.
b) I want easy syntaxing.


From what I read, PERL is what I'm looking for. But if I can do shell-scripting type of coding with Java, I'll stick with Java.

By the way...Can you do GUI apps with PERL using QT or something? I mean, besides web based apps?


Thank you guys,
RandomX
 
Old 11-25-2003, 12:55 PM   #2
zer0python
Member
 
Registered: Sep 2003
Posts: 104

Rep: Reputation: 20
Uh, your java question is kind of hard.. for me seeing as how I'm not the most advanced Java person around, you should check out the Sytem class (http://java.sun.com/j2se/1.4.2/docs/...ng/System.html) I'm sure if there is something, it'd be in there... anyway, yes, perl can create gui's using QT, you have to download a library though! I think that's, hope I answered both your questions...
 
Old 11-25-2003, 01:02 PM   #3
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
Check out the System.exec() function in the System class like zer0python said.
 
Old 11-25-2003, 01:24 PM   #4
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Original Poster
Rep: Reputation: 16
Can you give me a quick example on how to use System.exec() function in the System class?
For example, I want to untar the the file foo.tar. My system is linux 2.4.22 using BASH shell.

Code:
public class UntarringFiles 
{
       public static void main(String[] args)
      {
        System.out.println("I will untar the file foo for you sir....");

/* What do you put between these two lines ?*/

      System.out.println("All set. Come back again.");

      }
}
 
Old 11-25-2003, 01:28 PM   #5
zer0python
Member
 
Registered: Sep 2003
Posts: 104

Rep: Reputation: 20
my guess would be more or less ->

Code:
public class UntarringFiles
{
    public static void main(String[] args) {
        System.out.println("I will untar the file foo for you sir...");
        System.exec("tar xf foo.tar");
        System.out.println("All set. Come back again.");
    }
}
I'm not sure how accurate I am on that..worth a try though o.o

Last edited by zer0python; 11-25-2003 at 01:29 PM.
 
Old 11-25-2003, 01:38 PM   #6
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
I'm sorry. I'm a moron. I wasn't thinking. It's not System.exec, but rather Runtime.getRuntime().exec("command here");

Here's a link

Sorry for the mental lapse.
 
Old 11-25-2003, 01:39 PM   #7
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Original Poster
Rep: Reputation: 16
I got an error....any ideas what's going on?

Code:
$ javac UntarringFiles.java

UntarringFoo.java:6: cannot resolve symbol
symbol  : method exec (java.lang.String)
location: class java.lang.System
System.exec("tar xf foo.tar");
      ^
1 error
 
Old 11-25-2003, 01:43 PM   #8
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Original Poster
Rep: Reputation: 16
Another compiling error but now using your second suggestion....

Code:
UntarringFiles.java:6: unreported exception java.io.IOException; must be caught or declared to be thrown
   Runtime.getRuntime().exec("tar xf foo.tar");
                            ^
1 error
original code

Code:
public class UntarringFiles
{
public static void main(String[] args)
{
   System.out.println("I will untar the file foo for you sir...");
   Runtime.getRuntime().exec("tar xf foo.tar");
   System.out.println("All set. Come back again.");
}
}
 
Old 11-25-2003, 01:56 PM   #9
zer0python
Member
 
Registered: Sep 2003
Posts: 104

Rep: Reputation: 20
put it in a try clause

Code:
try {
  Runtime.getRuntime().exec("tar xf foo.tar");
} catch(Exception e) {}

Last edited by zer0python; 11-25-2003 at 01:59 PM.
 
Old 11-25-2003, 03:22 PM   #10
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Original Poster
Rep: Reputation: 16
It compiles ok. But I get a run-time error.

Code:
$ java UntarringFiles.java

Exception in thread "main" java.lang.NoClassDefFoundError: UntarringFiles/java
 
Old 11-25-2003, 03:49 PM   #11
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
java UntarringFiles

you should not have the .java extension when you run it.

Last edited by oulevon; 11-25-2003 at 03:50 PM.
 
Old 11-25-2003, 03:54 PM   #12
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Original Poster
Rep: Reputation: 16
oooops....my bad. I can't believe I missed that basic stuff.

it runs fine now..

complete code

Code:
public class UntarringFiles
{
public static void main(String[] args)
{
   System.out.println("I will untar the file foo for you sir...");
                                                                                                 
try {
  Runtime.getRuntime().exec("tar xf foo.tar");
} catch(Exception e) {}
                                                                                                 
   System.out.println("All set. Come back again.");
}
}
 
Old 11-25-2003, 04:01 PM   #13
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
Don't worry about it. I do it all the time, and I've been using java for a few years now. I'm glad it works.
 
Old 11-25-2003, 05:21 PM   #14
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Original Poster
Rep: Reputation: 16
Quote:
Don't worry about it. I do it all the time,
yeah...sometimes I don't know about myself man.

Do you know how to use PERL-TK or any similar PERL GUI development? my main concern with Java is speed. Speed writing it, running it, debugging it. But I kinda miss the GUI.

Can you use QT-Designer or Kdevelop to make GUI stuff in PERL? any thoughts on that? any Better PERL GUI development tool?
 
Old 11-25-2003, 05:48 PM   #15
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
The main gui module for Perl is Tk. It's pretty decent.

Though i still prefer Java over Perl as a true programming language. Mainly because i'm an oop whore
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is Java Desktop System really Java? NCC-1701&NCC-1701-D Programming 7 06-19-2005 03:55 PM
java, call function in calling object exodist Programming 9 06-13-2004 11:49 PM
Problem calling linux program from Java spasco Programming 3 04-25-2004 12:13 AM
calling command line functions in java darthczyz Programming 2 12-09-2003 07:01 AM
Java Commands Help!!!! thundersdm Programming 13 09-24-2003 04:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration