473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Run commands using java

77
Dear All

I have written a simple program using thread. My aim is to run a command. Please lead me how to do ? I am just posting my start code here.

Expand|Select|Wrap|Line Numbers
  1.  
  2. package com.pjerald.robot;
  3.  
  4. import java.net.*;
  5. import java.io.*;
  6.  
  7. public class RunCmd extends Thread
  8. {
  9. public static void main(String[] args)
  10. {
  11. //RunCmd rc = new RunCmd("ls -ltr");
  12. RunCmd rc = new RunCmd("mkdir -p test1/test2/test3");
  13. rc.start();
  14. }
  15.     String cmd = null;
  16.     RunCmd(String cmd) {
  17.         this.cmd = cmd;
  18.     }
  19.     public void run() {
  20.         if(cmd != null)
  21.         {
  22.             try{
  23.                 Thread t = new Thread(cmd);
  24.                 t.start();
  25.                 int count = 0;
  26.                 do
  27.                 {
  28.                     if(count == 100)
  29.                     {
  30.                         System.out.println(" TIME OUT for Thread ::"+t.getName());
  31.                         t.destroy();
  32.                     }
  33.                     t.sleep(500);
  34.                     System.out.println("Thread ::"+t.getName()+" running");
  35.                     count = count+1;
  36.                 }
  37.                 while(t.isAlive());
  38.             }
  39.             catch(Exception e)
  40.             {
  41.                 e.printStackTrace();
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47.  
  48.  
Aug 29 '08 #1
3 1943
JosAH
11,448 Expert 8TB
Your curly brackets are goofy and read the API documentation of the Thread
class because line 22 doesn't do what you assume it does.

kind regards,

Jos
Aug 30 '08 #2
pjerald
77
Thanks for the reply.

Your curly brackets are goofy and read the API documentation of the Thread
class because line 22 doesn't do what you assume it does.
Sorry for code indentation.

I read API documentation and understand that my constructor is equivalent to

Thread(null, null, name);
which is Thread(ThreadGroup group, Runnable target, String name)

I don know how to proceed with this. give an idea please.
i will try and get back to you.
Aug 30 '08 #3
JosAH
11,448 Expert 8TB
Thanks for the reply.



Sorry for code indentation.

I read API documentation and understand that my constructor is equivalent to

Thread(null, null, name);
which is Thread(ThreadGroup group, Runnable target, String name)

I don know how to proceed with this. give an idea please.
i will try and get back to you.
Well, it's not just the indentation, the structure of your methods is just plain wrong
(see the position of the curly brackets).

If you want to run an external program have a look at the Runtime.exec() methods.
Also read the API documentation for the Thread class: you can't destroy() a Thread
safely and the method is therefore deprecated.

kind regards,

Jos
Aug 30 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Simon Hawkins | last post by:
I seem to remember there's a java function for executing commands on the native system (UNIX in this case) but cannot remember which. Anyone know? Also, is it possible to grab the results of the...
3
by: Hal Vaughan | last post by:
I need to be able to run external commands from within a Java program, on Linux and Windows (and eventually OSX). Under most circumstances there is no problem, but if the path I specify as part of...
4
by: ÂÑTØÑ | last post by:
Hi, I was looking for a list of commands, but I can't find it. It's about commands you can type in the Internet Explorer adress bar, to get some information about a website. For instance...
2
by: Rich | last post by:
Hello, I have an oleDBDataAdapter (da1) which gets data from an Access mdb and fills a dataset (ds1) with integer data from "tbl1" in Access. Data displays in textboxes on a form. The...
5
by: Johannes Röckert | last post by:
Hi, can anyone help me on how to develop a program in c++ which does single step execution using a callback between the asm commands? I need something like debuggers do - a callback function...
3
by: Venkat Kuppa | last post by:
Hi all I am trying to remove files from stored procedure using Linux OS commands I know smoe udf needs to be developed either in c or java, but nowhere code is available can anyone forward me...
6
by: Peted | last post by:
Hi wondering what is the best way to do this Need a user to click a button, that sends 3 or 4 string based commands via a TCP/ip socket link I can connect to the ip device no problems, am...
1
by: Johny Begood | last post by:
Hi All, Anyone any suggestions on sending AT modem commands to a mobile phone connected via Bluetooth, primarly to send SMS. How would you get php to send these commands AT OK AT+CMGF=1 OK
0
by: douglandmesser | last post by:
Hey all. I found this awesome source code online that covers basically all of the wininet.dll function calls for FTP commands. It doesn't, however, actually work! lol The code is below: Source -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.