2 Questions: java calling system commands? PERL vs Java?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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...
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.");
}
}
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.
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.");
}
}
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.");
}
}
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.