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
5 | public static void main(String args[]) { |
8 | String command[] = new String[3]; |
9 | command[0] = "javac aPlusB.java"; |
10 | command[1] = "java aPlusB"; |
14 | Process p = Runtime.getRuntime().exec(command); |
17 | BufferedReader stdInput = new BufferedReader(new |
18 | InputStreamReader(p.getInputStream())); |
20 | BufferedReader stdError = new BufferedReader(new |
21 | InputStreamReader(p.getErrorStream())); |
23 | // read the output from the command |
24 | System.out.println("Here is the standard output of the command:\n"); |
25 | while ((s = stdInput.readLine()) != null) { |
26 | System.out.println(s); |
29 | // read any errors from the attempted command |
30 | System.out.println("Here is the standard error of the command (if any):\n"); |
31 | while ((s = stdError.readLine()) != null) { |
32 | System.out.println(s); |
37 | catch (IOException e) { |
38 | System.out.println("exception happened - here's what I know: "); |
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:
1
import java.io.*;
2
3
public class run {
4
5
public static void main(String args[]) {
6
String command = "javac aPlusB.java";
7
runCommand(command);
8
9
command = "java aPlusB 1 2";
10
runCommand(command);
11
12
}
13
14
public static void runCommand(String command) {
15
String s = null;
16
try {
17
Process p = Runtime.getRuntime().exec(command);
18
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
19
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
20
21
// read the output from the command
22
System.out.println("Here is the standard output of the command:\n");
23
while ((s = stdInput.readLine()) != null) {
24
System.out.println(s);
25
}
26
27
// read any errors from the attempted command
28
System.out.println("Here is the standard error of the command (if any):\n");
29
while ((s = stdError.readLine()) != null) {
30
System.out.println(s);
31
}
32
//System.exit(0);
33
} catch (IOException e) {
34
System.out.println("exception happened - here's what I know: ");
35
e.printStackTrace();
36
System.exit(-1);
37
}
38
}
39
}
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:
1
public class APlusB
2
{
3
public static void main( String args[] )
4
{
5
Scanner sc = new Scanner(System.in);
6
int a = sc.nextInt();
7
int b = sc.nextInt();
8
9
System.out.println(a+b);
10
11
}
12
}
Ə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