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
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);
}
}
}
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)
0
Exception-ın nə olduğunu yaza bilərsinizmi? Nə xəta verir?
3
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:
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); } } }0
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.
2
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]);
0
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:
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.
0
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.
2
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.
Sual verin
Cavab verin