LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   2 Questions: java calling system commands? PERL vs Java? (https://www.linuxquestions.org/questions/programming-9/2-questions-java-calling-system-commands-perl-vs-java-119830/)

randomx 11-25-2003 12:42 PM

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

zer0python 11-25-2003 12:55 PM

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...

oulevon 11-25-2003 01:02 PM

Check out the System.exec() function in the System class like zer0python said.

randomx 11-25-2003 01:24 PM

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.");

      }
}


zer0python 11-25-2003 01:28 PM

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

oulevon 11-25-2003 01:38 PM

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.

randomx 11-25-2003 01:39 PM

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


randomx 11-25-2003 01:43 PM

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.");
}
}


zer0python 11-25-2003 01:56 PM

put it in a try clause

Code:


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


randomx 11-25-2003 03:22 PM

It compiles ok. But I get a run-time error.

Code:

$ java UntarringFiles.java

Exception in thread "main" java.lang.NoClassDefFoundError: UntarringFiles/java


oulevon 11-25-2003 03:49 PM

java UntarringFiles

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

randomx 11-25-2003 03:54 PM

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.");
}
}


oulevon 11-25-2003 04:01 PM

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.

randomx 11-25-2003 05:21 PM

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?

coolman0stress 11-25-2003 05:48 PM

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 ;)


All times are GMT -5. The time now is 12:25 PM.