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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
|
11-25-2003, 12:42 PM
|
#1
|
Member
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130
Rep:
|
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
|
|
|
11-25-2003, 12:55 PM
|
#2
|
Member
Registered: Sep 2003
Posts: 104
Rep:
|
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...
|
|
|
11-25-2003, 01:02 PM
|
#3
|
Member
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438
Rep:
|
Check out the System.exec() function in the System class like zer0python said.
|
|
|
11-25-2003, 01:24 PM
|
#4
|
Member
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130
Original Poster
Rep:
|
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.");
}
}
|
|
|
11-25-2003, 01:28 PM
|
#5
|
Member
Registered: Sep 2003
Posts: 104
Rep:
|
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.
|
|
|
11-25-2003, 01:38 PM
|
#6
|
Member
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438
Rep:
|
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.
|
|
|
11-25-2003, 01:39 PM
|
#7
|
Member
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130
Original Poster
Rep:
|
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
|
|
|
11-25-2003, 01:43 PM
|
#8
|
Member
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130
Original Poster
Rep:
|
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.");
}
}
|
|
|
11-25-2003, 01:56 PM
|
#9
|
Member
Registered: Sep 2003
Posts: 104
Rep:
|
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.
|
|
|
11-25-2003, 03:22 PM
|
#10
|
Member
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130
Original Poster
Rep:
|
It compiles ok. But I get a run-time error.
Code:
$ java UntarringFiles.java
Exception in thread "main" java.lang.NoClassDefFoundError: UntarringFiles/java
|
|
|
11-25-2003, 03:49 PM
|
#11
|
Member
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438
Rep:
|
java UntarringFiles
you should not have the .java extension when you run it.
Last edited by oulevon; 11-25-2003 at 03:50 PM.
|
|
|
11-25-2003, 03:54 PM
|
#12
|
Member
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130
Original Poster
Rep:
|
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.");
}
}
|
|
|
11-25-2003, 04:01 PM
|
#13
|
Member
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438
Rep:
|
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.
|
|
|
11-25-2003, 05:21 PM
|
#14
|
Member
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130
Original Poster
Rep:
|
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?
|
|
|
11-25-2003, 05:48 PM
|
#15
|
Member
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288
Rep:
|
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 04:27 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|