jerhynsoen
jerhynsoen

Reputation: 215

batch code in java

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

Upvotes: 0

Views: 603

Answers (2)

jerhynsoen
jerhynsoen

Reputation: 215

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

Upvotes: 0

dbenham
dbenham

Reputation: 130849

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

" is escaped as \"

' is escaped as \'

\ is escaped as \\

Upvotes: 1

Related Questions