Need urgent help on the JAR matter!

Hello,
I have to write a program which puts all files in a certain directory into a *.jar distributive.
Preferably the user must have an opportunity to choose a directory;
more simple variant is with a current or fixed directory name.
Technically, it should just generate the command line for JAR.exe and then call it.

Could anyone help me with the source code for the problem?
One day is not enough to learn Java :(

Comments

  • Does it have to be a java app? Why not use ant with a simple jar target:






    ...






    Note: This is only a skeleton from memory - you need to fill in the compile details and correct the lib/classes paths.

    Check out http://ant.apache.org/ for more details on ant.

    : Hello,
    : I have to write a program which puts all files in a certain directory into a *.jar distributive.
    : Preferably the user must have an opportunity to choose a directory;
    : more simple variant is with a current or fixed directory name.
    : Technically, it should just generate the command line for JAR.exe and then call it.
    :
    : Could anyone help me with the source code for the problem?
    : One day is not enough to learn Java :(
    :

  • : Hello,
    : I have to write a program which puts all files in a certain directory into a *.jar distributive.
    : Preferably the user must have an opportunity to choose a directory;
    : more simple variant is with a current or fixed directory name.
    : Technically, it should just generate the command line for JAR.exe and then call it.
    :
    : Could anyone help me with the source code for the problem?
    : One day is not enough to learn Java :(
    :

    The following code does what I think you are looking for. Don't know how you plan to use it but you need to either pass the strings as args or create some gui.
    -----------------------------------------------------------------------
    //enter correct location of jar.exe
    String pathToJarEXE = "D:\Program Files\Java\jdk1.5.0\bin\";

    //name of jar file to be created
    String newJarFileName = "testJar.jar";

    //enter source of files to run jar.exe on
    String directoryToJar = "C:\Test";

    Runtime run = Runtime.getRuntime();

    try
    {
    // Call jar.exe
    Process process = run.exec(pathToJarEXE+"jar cf "+newJarFileName+
    " -C "+directoryToJar+" .");
    //output to command line should be similar to --
    //C:Program FilesJavajdk 1.5.0injar cf testJar.jar -C C:Test .

    process.waitFor();//allow it to complete

    process.destroy();//kill the thread
    }

    catch (Exception e)
    {
    System.out.println("Caught exception " + e.getMessage());
    }
    ------------------------------------------------------------------------

    Code stolen and modified from:
    http://java.sun.com/docs/books/tutorial/jar/basics/build.html
    http://www.personalmicrocosms.com/html/javafaq.html

    Hope this helps.
    Ashlungs
  • This is pretty brute force - be careful with Runtime.exec, it can really bite you with deadlock if you're spawning a proc that produces a lot of output to stdout or stderr. If you don't write this carefully (multithreaded with backgrounds that are clearing those buffers) the main thread will deadlock.
    Also - if you plan on having this code run for an extended period of time and you call the exec method over and over then remember that you must explicitely close all 3 streams (in,out,err) in a finally block. Unfortunately, java doesn't handle this well by itself. Your java process will end up starving your os of pipes.

    Note - I still think you should be looking at ant. If you really need more control then I'd think about perl for this problem...why start a JVM to execute a single cmd?

    : : Hello,
    : : I have to write a program which puts all files in a certain directory into a *.jar distributive.
    : : Preferably the user must have an opportunity to choose a directory;
    : : more simple variant is with a current or fixed directory name.
    : : Technically, it should just generate the command line for JAR.exe and then call it.
    : :
    : : Could anyone help me with the source code for the problem?
    : : One day is not enough to learn Java :(
    : :
    :
    : The following code does what I think you are looking for. Don't know how you plan to use it but you need to either pass the strings as args or create some gui.
    : -----------------------------------------------------------------------
    : //enter correct location of jar.exe
    : String pathToJarEXE = "D:\Program Files\Java\jdk1.5.0\bin\";
    :
    : //name of jar file to be created
    : String newJarFileName = "testJar.jar";
    :
    : //enter source of files to run jar.exe on
    : String directoryToJar = "C:\Test";
    :
    : Runtime run = Runtime.getRuntime();
    :
    : try
    : {
    : // Call jar.exe
    : Process process = run.exec(pathToJarEXE+"jar cf "+newJarFileName+
    : " -C "+directoryToJar+" .");
    : //output to command line should be similar to --
    : //C:Program FilesJavajdk 1.5.0injar cf testJar.jar -C C:Test .
    :
    : process.waitFor();//allow it to complete
    :
    : process.destroy();//kill the thread
    : }
    :
    : catch (Exception e)
    : {
    : System.out.println("Caught exception " + e.getMessage());
    : }
    : ------------------------------------------------------------------------
    :
    : Code stolen and modified from:
    : http://java.sun.com/docs/books/tutorial/jar/basics/build.html
    : http://www.personalmicrocosms.com/html/javafaq.html
    :
    : Hope this helps.
    : Ashlungs
    :

  • : This is pretty brute force - be careful with Runtime.exec, it can really bite you with deadlock if you're spawning a proc that produces a lot of output to stdout or stderr. If you don't write this carefully (multithreaded with backgrounds that are clearing those buffers) the main thread will deadlock.
    : Also - if you plan on having this code run for an extended period of time and you call the exec method over and over then remember that you must explicitely close all 3 streams (in,out,err) in a finally block. Unfortunately, java doesn't handle this well by itself. Your java process will end up starving your os of pipes.
    :
    : Note - I still think you should be looking at ant. If you really need more control then I'd think about perl for this problem...why start a JVM to execute a single cmd?
    :

    Good point Extatic. He's absolutely right about Runtime being tricky to work with. You might want to take a look at some programming references to handle all the potential problems. The page below has a brief discussion of things that can go wrong with Runtime.

    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

    I'm assuming you want to do this because you are adding a feature to an existing program, not trying to automate a process for yourself. If you are just automating something I'd follow Extatic's advice and not use Java. A script would make more sense.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories