Java Runtime.exec();

Salam. Menim bir dene run clasım var , onu istifafde ederek mən aPlusB.java faylını öncə compile elemək və sonra input olaraq iki rəqəm verib output - u check elemek istiyirəm. Aşağıda run classının kodu var [code] import java.io.*; public class run { public static void main(String args[]) { String s = null; String command[] = new String[3]; command[0] = "javac aPlusB.java"; command[1] = "java aPlusB"; command[2] = "1 2"; try { Process p = Runtime.getRuntime().exec(command); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); // read the output from the command System.out.println("Here is the standard output of the command:\n"); while ((s = stdInput.readLine()) != null) { System.out.println(s); } // read any errors from the attempted command System.out.println("Here is the standard error of the command (if any):\n"); while ((s = stdError.readLine()) != null) { System.out.println(s); } System.exit(0); } catch (IOException e) { System.out.println("exception happened - here's what I know: "); e.printStackTrace(); System.exit(-1); } } } [/code] Sadəcə olaraq run eliyəndə Exception verir. Problemi necə həll elemık olar ?

Verilmiş cavablar və yazılan şərhlər (7 cavab var)

Ramin Orucov (2011-05-16 18:04:43)
Bu proqramda a və b-ni input-dan oxuduğunuza görə onları əsas proqramdan İnputStream ilə göndərmək lazımdır. Vaxt olanda nümunə kod yazıb göstərərəm.

Xeyyam Quliyev (2011-05-16 14:06:29)
public class APlusB { public static void main( String args[] ) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); System.out.println(a+b); } } Əsas məqsəd bu tipli programı test etmekdi.

Xeyyam Quliyev (2011-05-16 13:59:58)
Burada məqsəd a+b -ni test etmek deyil. Programlama olimpiadalarında məsələnin şərti verilir, və həmin şərtə əsasən iştirakçılar istənilən programlama dilində program yazıb göndərirlər testing üçün. İndi təsəvvür edin ki, məsələnin şərti deyir ki, a və b ədədləri giriləcək , siz de onların cəmini cıxaracaq java dilində program yazın. təbii ki, kodlar bu formada olacaq: [code] public class APlusB { public static void main( String args[] ) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); System.out.println(a+b); } } [/code] Əsas məqsəd bu tipli programı test etmekdi.

Ramin Orucov (2011-05-16 09:19:40)
Main metoduna göndərilən arqumentlər "String[] args" massiv kimi göndərilir. Siz öz proqramınızda a və b-nı aşağıdakı kimi integerə çevirin. a = Integer.parseInt(args[0]); b = Integer.parseInt(args[1]);

Xeyyam Quliyev (2011-05-14 16:27:04)
Cavabınıza görə təşəkkürlər . Ancaq yenede çalışmadı , daha doğrusu compile oldu ancaq "java aPlusB 1 2" hissəsi çalışmadı . Bir də ki, mən səhv etmirəmsə "java aPlusB 1 2" runCommand -a göndərəndə 1 və 2 argument kimi verilir aPlusB klasına. Ancaq mənim aPlusB.java klasım a və b integerlərini input kimi qəbul etməlidi.

İlkin Esrefli (2011-05-14 13:56:45)
Problem ona görədir ki, Runtime.getRuntime().exec() metodu yalnız 1 comand qəbul edə bilir, sizdə isə 2 dənədi "javac" və "java". Eyni zamanda array olmadan da etmək olar. run classınızı bu şəkildə yaza bilərsiniz: [code] import java.io.*; public class run { public static void main(String args[]) { String command = "javac aPlusB.java"; runCommand(command); command = "java aPlusB 1 2"; runCommand(command); } public static void runCommand(String command) { String s = null; try { Process p = Runtime.getRuntime().exec(command); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); // read the output from the command System.out.println("Here is the standard output of the command:\n"); while ((s = stdInput.readLine()) != null) { System.out.println(s); } // read any errors from the attempted command System.out.println("Here is the standard error of the command (if any):\n"); while ((s = stdError.readLine()) != null) { System.out.println(s); } //System.exit(0); } catch (IOException e) { System.out.println("exception happened - here's what I know: "); e.printStackTrace(); System.exit(-1); } } } [/code]

İlkin Esrefli (2011-05-14 11:17:23)
Exception-ın nə olduğunu yaza bilərsinizmi? Nə xəta verir?

Mövzu üzrə bənzər suallara da baxa bilərsiniz.

Apk-ya replaceAll() əlavə etmək

JSF də kömək lazımdır

proqramlaşdırma üçün vəsaitlər

Java-da proqram çalışdırmaq

Javada void nədir

Java Event Dispatch Thread