/
Run as Java Program
executable file
·136 lines (119 loc) · 3.35 KB
/
Run as Java Program
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
#!/bin/sh
# Bill Chatfield
# GPL 2.0
# Run as Java Program
# Script for Nautilus file manager in Gnome in Linux.
# Install into $HOME/.local/share/nautilus/scripts
message()
{
local msg="$*"
zenity --info --text="$msg" --title="Run as Java Program"
}
error()
{
local msg="$*"
zenity --error --text="$msg" --width=300 --height=200 --title="Run as Java Program - Error"
}
showFileContents()
{
local fileName="$1"
shift
zenity --text-info --filename="$fileName" --width=850 --height=500 --title="Run as Java Program - Output from: $*"
}
run()
{
local tmpTmpl=run-as-java-output.XXXXXXXXXX
local outputFile=$(mktemp -t $tmpTmpl)
local exitCode
"$@" > "$outputFile" 2>&1
exitCode=$?
if [ -s "$outputFile" ]
then
if grep -q "no main manifest attribute" "$outputFile"
then
error Jar file does not contain a runnable program.
else
showFileContents "$outputFile" "$@"
fi
elif [ $exitCode -ne 0 ]
then
error Command failed without producing any output: exit-code=$exitCode command="$@"
fi
rm "$outputFile"
}
runWithTerminal()
{
if which gnome-terminal
then
run gnome-terminal -- "$@"
else
error Could not find gnome-terminal command. It is currently the only terminal supported. Modify $0 to fix it.
fi
}
processOneFile()
{
local extn="${1##*.}"
local dirn="$(dirname "$1")"
local bnam="$(basename "$1")"
local clas="${bnam%%.*}"
if [ "$extn" = 'jar' ]
then
run "$JAVA" -jar "$1"
elif [ "$extn" = 'class' ]
then
runWithTerminal "$JAVA" -cp "$dirn" "$clas"
elif [ "$extn" = 'java' ]
then
( cd "$dirn"
`dirname "$JAVA"`/javac "$bnam"
runWithTerminal "$JAVA" -cp . "$clas"
)
else
error $bnam is not the right type of file to run as a Java \
program. To run a file as a Java program it must have one \
of these extensions: jar, class, \
or java. If it is a \"jar\" file, it must \
contain a main class that is configured in its MANIFEST.MF \
file. If it is a \"class\" or \"java\" file, it must have \
a main method. Otherwise it will fail to run.
fi
}
# Handle possible call from "Run as Java Program with Alternat Java"
if [ -e "$ALT_JAVA" ]
then
JAVA="$ALT_JAVA"
elif [ -d "$JAVA_HOME" ]
then
JAVA="$JAVA_HOME"/bin/java
else
JAVA=java
fi
# Note on NAUTILUS_SCRIPT_SELECTED_FILE_PATHS variable provided as by
# Nautilus:
#
# Spaces can separate file names and also be included in file names.
# The spaces that separate file names will be followed by a slash
# character because each file name is given in its absolute form.
# This loop builds up file names from their space-separated parts until it
# finds a combination that is actually a file that exists. This algorithm
# effectively determines whether the spaces are part of the file name or
# are separators between the file names.
unset FILE_NAME
for PART in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
do
if [ -z "$FILE_NAME" ]
then
FILE_NAME=$PART
else
FILE_NAME="$FILE_NAME $PART"
fi
if [ -f "$FILE_NAME" ]
then
processOneFile "$FILE_NAME"
unset FILE_NAME
fi
done
if [ -n "$FILE_NAME" ]
then
error Could not interpret file name: $FILE_NAME
fi