0

I do not want to just call a batch file from java, I want the code to be in java.

I have this so far but my batch file has a lot of code and does not accept it.

public static void main(String[] args) { 
    final String dosCommand = "cmd /c dir /s";
    final String location = "C:\\WINDOWS";
    try {
      final Process process = Runtime.getRuntime().exec(dosCommand + " " + location);
      final InputStream in = process.getInputStream();
      int ch;
      while((ch = in.read()) != -1) {
        System.out.print((char)ch);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
}

Sample of the batch code I want to add in my java class. I am not adding the @echo off to my class, unless someone tells me I need to.

@echo off

rundll32 wbemupgd, UpgradeRepository

NET USE Q: \\Somenetworkpath\ /PERSISTENT:NO
Q:
CD \DeskTop\Troubleshoot\
COPY subinacl.msi "C:\Documents and Settings\%USERNAME%\Desktop"
C:
MSIEXEC.EXE /i "\Documents and Settings\%USERNAME%\Desktop\subinacl.msi" /qn

DEL /Q "C:\Documents and Settings\%USERNAME%\Desktop\subinacl.msi"

C:
CD \Program Files\Windows Resource Kits\Tools\
SUBINACL /SUBKEYREG HKEY_LOCAL_MACHINE /GRANT=Administrators=F
SUBINACL /SUBKEYREG HKEY_CURRENT_USER /GRANT=Administrators=F
SUBINACL /SUBKEYREG HKEY_CLASSES_ROOT /GRANT=Administrators=F
SUBINACL /SUBDIRECTORIES %SystemDrive% /GRANT=Administrators=F
SUBINACL /SUBKEYREG HKEY_LOCAL_MACHINE /GRANT=System=F
SUBINACL /SUBKEYREG HKEY_CURRENT_USER /GRANT=System=F
SUBINACL /SUBKEYREG HKEY_CLASSES_ROOT /GRANT=System=F
SUBINACL /SUBDIRECTORIES %SystemDrive% /GRANT=System=F
6
  • When you say 'does not accept it', what do you mean? Your output is a bit nonsensical. Commented Aug 7, 2012 at 18:59
  • Do you have a valid reason for embedding a batch script inside a Java file? Seems to me like an example of a bad practice to create such a mix. Having them separate would allow you to change the script without recompiling the Java class. Commented Aug 7, 2012 at 19:31
  • @machinery I just want to have less files if possible and do not know another way to rethink the batch file in java. Recompiling the java file is not an issue for me, it's prefered to prevent modification. Commented Aug 7, 2012 at 20:10
  • @nathaniel If i copy and paste the code I get a lot of errors, mostly syntax, even though it is surrounded by quotes. For example one of the errors dislikes the use of "\" and other syntax errors. Commented Aug 7, 2012 at 20:22
  • @jerhynsoen Those errors are probably going to be as useful as the output for achieving your end goal. You should probably also post what you expect to get out. Commented Aug 7, 2012 at 20:45

2 Answers 2

1

I suspect you have not escaped special characters in your batch code properly for java.

" is escaped as \"

' is escaped as \'

\ is escaped as \\

Sign up to request clarification or add additional context in comments.

Comments

0

What I wanted does not seem possible. Ended up just calling the batch file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.