Re: runajar anywhere in the world
"Zahd Rahman" <[email protected]> Sat, 22 Feb 2003 16:44:56 -0000
| Newsgroups | gmane.comp.web.jigsaw |
|---|---|
| Message-ID | <004701c2da91$bb851790$3471fea9@objectwoszbz07> |
This is a multi-part message in MIME format.
------=_NextPart_000_0044_01C2DA91.BAF3D410
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
upload file with multipartWhat do you think of my runajar anywhere in =
the world ?
/*
* runjar.java
*
* Created on 22 February 2003, 16:12
*/
package runajar;
/**
*
* @author Zahid Rahman
Copyright - All rights reserved
*/
import java.net.*;
import java.io.*;
/**
* <p>Title: run a jar <u>from</u> anywhere in the world</p>
* <p>Description: java -jar runajar.jar <url or path of the jar to run> =
<arguments>
* or: runajar <url or path of the jar to run> =
<arguments></p>
* <p>Company: objectworlds</p>
* @author zahid
* @version 1.0
*
*/
public class Runajar {
static final int BLOCK_SIZE =3D 65500; // bytes
static byte[] buf =3D new byte[BLOCK_SIZE];
static final int SLEEP_UNIT =3D 333; // msec
static final String TMP_DIR =3D "/tmp/runjar";
public static void main(String[] args) {
try {
if (args.length > 0) {
String urlstring =3D args[0]; // jar url string
File here =3D new File(".");
File localcopy =3D new File(urlstring);
if (!localcopy.exists()) {
URL url =3D new URL(urlstring);
String filename =3D =
urlstring.substring(urlstring.lastIndexOf('/') + 1); // jar name
File root =3D new File(here.getAbsolutePath()); // is not it =
something?
while (root.getParent() !=3D null && root.getParent() !=3D =
root.getCanonicalPath()) {
root =3D new File(root.getParent()); // that's the way to =
find the root
}
File tmpdir =3D new File(root, TMP_DIR);
if (!tmpdir.exists()) tmpdir.mkdirs();
localcopy =3D new File(tmpdir, filename); // local copy of the =
jar
InputStream is =3D new BufferedInputStream(url.openStream());
OutputStream os =3D new BufferedOutputStream(new =
FileOutputStream(localcopy));
pipe(is, os, true);
os.close();
is.close();
}
String javaexec =3D System.getProperty("java.home") + =
File.separator +
"bin" + File.separator +
"java";
String cmd =3D javaexec + " -jar " + =
localcopy.getAbsolutePath();
for (int i =3D 1; i < args.length; i++) {
cmd +=3D """ + args[i] + """;
}
Process process =3D Runtime.getRuntime().exec(cmd, null, here);
int result =3D 0;
for (boolean isRunning =3D true; isRunning;) {
Thread.sleep(SLEEP_UNIT);
try {
result =3D process.exitValue();
isRunning =3D false;
} catch (IllegalThreadStateException ie) {}
try {
pipe(System.in, process.getOutputStream(), false);
pipe(process.getErrorStream(), System.err, false);
pipe(process.getInputStream(), System.out, false);
} catch(Exception e) {}
}
System.exit(result);
}
} catch (Exception e) {
System.err.println(e);
}
}
static void pipe(InputStream in, OutputStream out, boolean isBlocking) =
throws IOException {
int nread;
int navailable;
int total =3D 0;
synchronized (in) {
while((navailable =3D isBlocking ? Integer.MAX_VALUE : =
in.available()) > 0 &&
(nread =3D in.read(buf, 0, Math.min(buf.length, =
navailable))) >=3D 0) {
out.write(buf, 0, nread);
total +=3D nread;
}
}
out.flush();
}
}
running instructions
runajar http://www.objectworlds/myapp.jar
Best Regards,
Zahid Rahman
http://www.objectworlds.com
------=_NextPart_000_0044_01C2DA91.BAF3D410
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>upload file with multipart</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial size=3D2>What do you think of my runajar =
anywhere in the=20
world ?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>/*<BR> * =
runjar.java<BR> *<BR> *=20
Created on 22 February 2003, 16:12<BR> */</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>package runajar;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>/**<BR> *<BR> * =
@author Zahid=20
Rahman</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> Copyright - All rights=20
reserved<BR> */</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>import java.net.*;<BR>import=20
java.io.*;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>/**<BR> * <p>Title: run a =
jar=20
<u>from</u> anywhere in the world</p><BR> *=20
<p>Description: java -jar runajar.jar <url or path of the jar =
to=20
run>=20
<arguments><BR> * &nb=
sp; =20
or: runajar <url or path of the jar to run>=20
<arguments></p><BR> * <p>Company:=20
objectworlds</p><BR> * @author zahid<BR> * @version=20
1.0<BR> *<BR> */</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>public class Runajar {</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> static final int BLOCK_SIZE =3D =
65500; //=20
bytes<BR> static byte[] buf =3D new byte[BLOCK_SIZE];<BR> =
static final=20
int SLEEP_UNIT =3D 333; // msec<BR> static final =
String TMP_DIR=20
=3D "/tmp/runjar";</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> public static void main(String[] =
args)=20
{<BR> try {<BR> if =
(args.length=20
> 0) {<BR> String urlstring =
=3D=20
args[0]; // jar url string<BR> =
File=20
here =3D new File(".");<BR> =
File=20
localcopy =3D new File(urlstring);</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial =
size=3D2> if=20
(!localcopy.exists())=20
{<BR> URL url =3D =
new=20
URL(urlstring);<BR> =
String=20
filename =3D urlstring.substring(urlstring.lastIndexOf('/') + 1); // jar =
name<BR> File root =
=3D new=20
File(here.getAbsolutePath()); // is not it=20
something?<BR> =
while=20
(root.getParent() !=3D null && root.getParent() !=3D=20
root.getCanonicalPath())=20
{<BR> =
root =3D=20
new File(root.getParent()); // that's the way to find the=20
root<BR> =20
}<BR> File tmpdir =
=3D new=20
File(root, =
TMP_DIR);<BR> =20
if (!tmpdir.exists()) tmpdir.mkdirs();</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial=20
size=3D2> =
localcopy =3D new=20
File(tmpdir, filename); // local copy of the=20
jar<BR> =
InputStream is =3D=20
new=20
BufferedInputStream(url.openStream());<BR> &=
nbsp; =20
OutputStream os =3D new BufferedOutputStream(new=20
FileOutputStream(localcopy));<BR> &nbs=
p; =20
pipe(is, os, =
true);<BR> =20
os.close();<BR> =20
is.close();<BR> =20
}<BR> String javaexec =3D=20
System.getProperty("java.home") + File.separator=20
+<BR> &n=
bsp; =20
"bin" + File.separator=20
+<BR> &n=
bsp; =20
"java";<BR> String cmd =
=3D =20
javaexec + " -jar " + localcopy.getAbsolutePath();</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial =
size=3D2> for (int=20
i =3D 1; i < args.length; i++)=20
{<BR> cmd +=3D """ =
+ args[i]=20
+ """;<BR> =20
}<BR> Process process =3D=20
Runtime.getRuntime().exec(cmd, null,=20
here);<BR> int result =3D=20
0;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial =
size=3D2> for=20
(boolean isRunning =3D true; isRunning;)=20
{<BR> =20
Thread.sleep(SLEEP_UNIT);<BR> &n=
bsp; =20
try =
{<BR> =20
result =3D=20
process.exitValue();<BR> &=
nbsp; =20
isRunning =3D =
false;<BR> }=20
catch (IllegalThreadStateException ie) {}</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial=20
size=3D2> try=20
{<BR> =20
pipe(System.in, process.getOutputStream(),=20
false);<BR> &n=
bsp;=20
pipe(process.getErrorStream(), System.err,=20
false);<BR> &n=
bsp;=20
pipe(process.getInputStream(), System.out,=20
false);<BR> }=20
catch(Exception e) {}<BR> =20
}<BR> =20
System.exit(result);<BR> =
}<BR> }=20
catch (Exception e) {<BR> =20
System.err.println(e);<BR> }<BR> }</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> static void pipe(InputStream in, =
OutputStream out, boolean isBlocking) throws IOException =
{<BR> =20
int nread;<BR> int navailable;<BR> =
int total=20
=3D 0;<BR> synchronized (in) =
{<BR> =20
while((navailable =3D isBlocking ? Integer.MAX_VALUE : in.available()) =
> 0=20
&&<BR>  =
; =20
(nread =3D in.read(buf, 0, Math.min(buf.length, navailable))) >=3D 0) =
{<BR> out.write(buf, 0,=20
nread);<BR> total +=3D=20
nread;<BR> }<BR> =20
}<BR> out.flush();<BR> }<BR>}<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>running instructions</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>runajar <A=20
href=3D"http://www.objectworlds/myapp.jar">http://www.objectworlds/myapp.=
jar</A></FONT></DIV>
<DIV><FONT face=3DArial size=3D2> </DIV></FONT>
<DIV><FONT face=3DArial size=3D2>Best Regards,</FONT><FONT face=3DArial=20
size=3D2></DIV></FONT>
<DIV><FONT face=3DArial size=3D2>Zahid</FONT> <FONT face=3DArial=20
size=3D2>Rahman</FONT></DIV>
<DIV><FONT face=3DArial size=3D2><A=20
href=3D"http://www.objectworlds.com">http://www.objectworlds.com</A></FON=
T></DIV></FONT></DIV></BODY></HTML>
------=_NextPart_000_0044_01C2DA91.BAF3D410--