1

I have an interesting issue. I have a Java application, that creates a batch file and the batch file sets a variable. Now, the application its self is being called from another batch file. Hence I would like to some how be able to pass the variable to the parent Batch file. I can do this while a batch calls another batch, but not when the batch file calls a jar file from, whom intern creates a Batch file, executes it, and exits. here is my code.

    import java.awt.Window;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import java.security.InvalidAlgorithmParameterException;
    import java.security.InvalidKeyException;

    import javax.crypto.BadPaddingException;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.ShortBufferException;

    import junit.framework.Test;

    public class main 
    {
        private static byte[] KeyBytes = {0,0,0,0,0,0,0,0};
        private static byte[] ivBytes = {0,0,0,0,0,0,0,0};

        public static void main(String[] args) throws InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, ShortBufferException, BadPaddingException, IOException, ClassNotFoundException, InterruptedException
        {   
            ObjectCrypter EncStr = new ObjectCrypter(KeyBytes,ivBytes);
            if(args.length == 2 )
            {
                if (args[0].equals("-E")|args[0].equals("-e"))
                {
                    byte[] tbyte = EncStr.encrypt(args[1]);
                    String test = byteToString(tbyte); 
                    System.out.println(test);
                }
                else if (args[0].equals("-D")|args[0].equals("-d"))
                {
                    byte[] t = stringToByte(args[1]);
                    writeBat("SET HypPWD=" +String.valueOf(EncStr.decrypt(t)));
                    //System.out.println("cmd /C " + getPath() + "tmpPwd.bat");
                    Process proc = Runtime.getRuntime().exec("cmd /C start tmpPwd.bat");
                    proc
                    proc.waitFor();
                }
            }
            else
            {
                System.out.println("Wrong Argument set please revise input");
                System.exit(0);
            }
        }
        private static String getPath() throws UnsupportedEncodingException 
        {
//....
        }
        public static String byteToString(byte[] b) 
        {
//....
        }
        public static byte[] stringToByte(String s)
        {
//....
        }
        public static void writeBat(String txt)
          {
              try
              {
                  FileWriter fstream = new FileWriter("tmpPwd.bat");
                  BufferedWriter out = new BufferedWriter(fstream);
                  out.write("@ECHO OFF");
                  out.newLine();
                  out.write(txt);
                  out.newLine();
                  out.write("EXIT");
                  out.newLine();
                  out.write("DEL tmpPwd.bat");
                  out.close();
              }
              catch (Exception e)
              {
                  System.err.println("Error: " + e.getMessage());
              }
          }
  • Are you still interested in the answer, or did you work it out? Are you saying you have: parent.bat, runs java app, runs child.bat. Child.bat exits, eg with 42. java app exits with 42. Parent.bat sees 42? – azhrei Jul 13 '12 at 04:10

0 Answers0