-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava.java
More file actions
97 lines (90 loc) · 3.49 KB
/
Java.java
File metadata and controls
97 lines (90 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package Utils;
import UIL.base.IImage;
import java.io.*;
import java.util.Scanner;
public class Java implements IImage {
public final IImage icon;
public final File file;
public final String name, version;
public Java(final IImage icon, final File file) throws IOException, InterruptedException {
this.file = file;
this.icon = icon;
final Process p = Runtime.getRuntime().exec(new String[] { file.getAbsolutePath(), "-version" });
p.getOutputStream().close();
if (p.waitFor() != 0)
throw new IOException("Exit code: " + p.exitValue());
try (final Scanner r = new Scanner(p.getErrorStream(), "UTF-8")) {
if (r.hasNextLine()) {
String l = r.nextLine();
int i = l.indexOf(" version \"");
if (i != -1) {
name = l.substring(0, i);
l = l.substring(i + 10);
i = l.indexOf("\"");
if (i != -1) {
version = l.substring(0, i);
return;
}
}
}
}
throw new IOException("No information");
}
public Java(final File file) throws IOException, InterruptedException {
this.file = file;
final Process p = Runtime.getRuntime().exec(new String[] { file.getAbsolutePath(), "-version" });
p.getOutputStream().close();
if (p.waitFor() != 0) {
System.out.println(file);
try (final Scanner s = new Scanner(p.getInputStream())) {
while (s.hasNextLine())
System.out.println(s.nextLine());
} catch (final Exception ex) {
ex.printStackTrace();
}
try (final Scanner s = new Scanner(p.getErrorStream())) {
while (s.hasNextLine())
System.out.println(s.nextLine());
} catch (final Exception ex) {
ex.printStackTrace();
}
throw new IOException("Exit code: " + p.exitValue());
}
try (final Scanner r = new Scanner(p.getErrorStream(), "UTF-8")) {
if (r.hasNextLine()) {
String l = r.nextLine();
int i = l.indexOf(" version \"");
if (i != -1) {
final String n = l.substring(0, i);
switch (n) {
case "java":
name = "JRE";
break;
case "openjdk":
name = "OpenJDK";
break;
default:
name = n;
break;
}
l = l.substring(i + 10);
i = l.indexOf("\"");
if (i != -1) {
version = l.substring(0, i);
icon = JavaSupport.JAVA_ICON;
return;
}
}
}
}
throw new IOException("No information");
}
@Override public Object getImage() { return icon == null ? null : icon.getImage(); }
@Override public String toString() { return name + " " + version; }
@Override
public boolean equals(final Object obj) {
return obj instanceof Java ?
file.getAbsolutePath().equals(((Java) obj).file.getAbsolutePath()) :
super.equals(obj);
}
}