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>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>/*<BR>&nbsp;* =
runjar.java<BR>&nbsp;*<BR>&nbsp;*=20
Created on 22 February 2003, 16:12<BR>&nbsp;*/</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>package runajar;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>/**<BR>&nbsp;*<BR>&nbsp;* =
@author&nbsp;&nbsp;Zahid=20
Rahman</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp; Copyright - All rights=20
reserved<BR>&nbsp;*/</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>import java.net.*;<BR>import=20
java.io.*;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>/**<BR>&nbsp;* &lt;p&gt;Title: run a =
jar=20
&lt;u&gt;from&lt;/u&gt; anywhere in the world&lt;/p&gt;<BR>&nbsp;*=20
&lt;p&gt;Description: java -jar runajar.jar &lt;url or path of the jar =
to=20
run&gt;=20
&lt;arguments&gt;<BR>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;=20
or: runajar &lt;url or path of the jar to run&gt;=20
&lt;arguments&gt;&lt;/p&gt;<BR>&nbsp;* &lt;p&gt;Company:=20
objectworlds&lt;/p&gt;<BR>&nbsp;* @author zahid<BR>&nbsp;* @version=20
1.0<BR>&nbsp;*<BR>&nbsp;*/</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>public class Runajar {</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; static final int BLOCK_SIZE =3D =
65500; //=20
bytes<BR>&nbsp; static byte[] buf =3D new byte[BLOCK_SIZE];<BR>&nbsp; =
static final=20
int SLEEP_UNIT =3D 333;&nbsp;&nbsp; // msec<BR>&nbsp; static final =
String TMP_DIR=20
=3D "/tmp/runjar";</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; public static void main(String[] =
args)=20
{<BR>&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if =
(args.length=20
&gt; 0) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String urlstring =
=3D=20
args[0]; // jar url string<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
File=20
here =3D new File(".");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
File=20
localcopy =3D new File(urlstring);</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial =
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if=20
(!localcopy.exists())=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; URL url =3D =
new=20
URL(urlstring);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
 String=20
filename =3D urlstring.substring(urlstring.lastIndexOf('/') + 1); // jar =

name<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; File root =
=3D new=20
File(here.getAbsolutePath()); // is not it=20
something?<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
while=20
(root.getParent() !=3D null &amp;&amp; root.getParent() !=3D=20
root.getCanonicalPath())=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
root =3D=20
new File(root.getParent()); // that's the way to find the=20
root<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; File tmpdir =
=3D new=20
File(root, =
TMP_DIR);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
if (!tmpdir.exists()) tmpdir.mkdirs();</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
localcopy =3D new=20
File(tmpdir, filename); // local copy of the=20
jar<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
InputStream is =3D=20
new=20
BufferedInputStream(url.openStream());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;=20
OutputStream os =3D new BufferedOutputStream(new=20
FileOutputStream(localcopy));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;=20
pipe(is, os, =
true);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
os.close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
is.close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String javaexec =3D=20
System.getProperty("java.home") + File.separator=20
+<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
"bin" + File.separator=20
+<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
"java";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String cmd =
=3D&nbsp;=20
javaexec + " -jar " + localcopy.getAbsolutePath();</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial =
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int=20
i =3D 1; i &lt; args.length; i++)=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd +=3D """ =
+ args[i]=20
+ """;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Process process =3D=20
Runtime.getRuntime().exec(cmd, null,=20
here);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int result =3D=20
0;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial =
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for=20
(boolean isRunning =3D true; isRunning;)=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
Thread.sleep(SLEEP_UNIT);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;=20
try =
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
result =3D=20
process.exitValue();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;=20
isRunning =3D =
false;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }=20
catch (IllegalThreadStateException ie) {}</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
pipe(System.in, process.getOutputStream(),=20
false);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;=20
pipe(process.getErrorStream(), System.err,=20
false);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;=20
pipe(process.getInputStream(), System.out,=20
false);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }=20
catch(Exception e) {}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
System.exit(result);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
}<BR>&nbsp;&nbsp;&nbsp; }=20
catch (Exception e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
System.err.println(e);<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; static void pipe(InputStream in, =

OutputStream out, boolean isBlocking) throws IOException =
{<BR>&nbsp;&nbsp;&nbsp;=20
int nread;<BR>&nbsp;&nbsp;&nbsp; int navailable;<BR>&nbsp;&nbsp;&nbsp; =
int total=20
=3D 0;<BR>&nbsp;&nbsp;&nbsp; synchronized (in) =
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
while((navailable =3D isBlocking ? Integer.MAX_VALUE : in.available()) =
&gt; 0=20
&amp;&amp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;=20
(nread =3D in.read(buf, 0, Math.min(buf.length, navailable))) &gt;=3D 0) =

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out.write(buf, 0,=20
nread);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; total +=3D=20
nread;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;=20
}<BR>&nbsp;&nbsp;&nbsp; out.flush();<BR>&nbsp; }<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>&nbsp;</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>&nbsp;<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--