-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
159 lines (139 loc) · 3.61 KB
/
main.cpp
File metadata and controls
159 lines (139 loc) · 3.61 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "process.h"
#include <iostream>
#include <string>
#include <glob.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifndef JARFILE
#error No JARFILE defined!
#endif
using namespace std;
#ifdef EMBEDDED_JAR_FILE
// see http://stackoverflow.com/questions/6785214/how-to-embed-a-file-into-an-executable-file and http://gareus.org/wiki/embedding_resources_in_executables
extern const unsigned char _binary_embedded_jar_file_start[];
extern const unsigned char _binary_embedded_jar_file_end[];
#endif
int runJava(string &javaBin, string &jarfile) {
char * const args[] = { (char *)javaBin.c_str(),
(char *)"-jar",
(char *)jarfile.c_str(),
NULL };
#ifndef QUIET
cerr << "Trying " << javaBin << endl;
#endif
Process p(args[0], args, false);
return p.wait();
}
bool fileExists(string &file) {
struct stat buf;
if (stat(file.c_str(), &buf) == -1) {
return false;
}
return true;
}
char *tmpjar = NULL;
static pid_t parentpid = getpid();
void cleanup(void) {
if (getpid() != parentpid) {
return;
}
if (tmpjar != NULL) {
cerr << "deleting " << tmpjar << endl;
unlink(tmpjar);
}
}
int main(int argc, char *argv[])
{
#ifdef EMBEDDED_JAR_FILE
tmpjar = strdup("/tmp/tmpjarfileXXXXXX");
int fd = mkstemp(tmpjar);
size_t len = _binary_embedded_jar_file_end - _binary_embedded_jar_file_start;
cerr << "creating " << tmpjar << " with " << len << " bytes" << endl;
write(fd, _binary_embedded_jar_file_start, len);
fsync(fd);
atexit(cleanup);
string jarfile = tmpjar;
#else
string jarfile = JARFILE;
#endif
int numJREs = 0;
// if the environment variable JAVA_HOME is set, we try this jvm
char *java_home = getenv("JAVA_HOME");
if (java_home != NULL)
{
string javaBin = java_home;
javaBin += "/bin/java";
if (fileExists(javaBin)) {
++numJREs;
if (runJava(javaBin, jarfile) == 0) {
return 0;
}
}
}
// then, we'll try the PATH
char *paths = getenv("PATH");
if (paths != NULL) {
paths = strdup(paths);
char *path = strtok(paths, ":");
while (path != NULL) {
string javaBin = path;
javaBin += "java";
if (fileExists(javaBin)) {
++numJREs;
if (runJava(javaBin, jarfile) == 0) {
return 0;
}
}
path = strtok(NULL, ":");
}
free(paths);
}
// try any jvm in /usr/lib/jvm/*/bin/java
glob_t globbuf = { 0, NULL, 0};
if (glob("/usr/lib/jvm/*/bin/java", 0, NULL, &globbuf) == 0) {
for (int i=0; i<globbuf.gl_pathc; ++i) {
string javaBin = globbuf.gl_pathv[i];
if (fileExists(javaBin)) {
++numJREs;
if (runJava(javaBin, jarfile) == 0) {
return 0;
}
}
}
}
globfree(&globbuf);
// see if we can find the jvm via update-alternatives
char *args[] = { (char *)"/usr/bin/update-alternatives",
(char *)"--list",
(char *)"java",
NULL };
Process p(args[0], args);
string javaBin;
while (p.getline(javaBin) > 0) {
if (fileExists(javaBin)) {
++numJREs;
if (runJava(javaBin, jarfile) == 0) {
return 0;
}
}
}
char *errmsg = (char *)"Could not launch "JARFILE;
if (numJREs == 0)
{
errmsg = (char *)"Java is not installed!";
}
cerr << errmsg << endl;
if (getenv("DISPLAY") != NULL)
{
char *args[] = { (char *)"/usr/bin/xmessage",
(char *)"-center",
errmsg,
NULL };
Process p(args[0], args);
p.wait();
}
return 1;
}