Jak uruchomić plik wsadowy z mojej aplikacji Java?
W mojej aplikacji Java chcę uruchomić plik wsadowy, który wywołuje "scons -Q implicit-deps-changed build\file_load_type export\file_load_type
"
Wygląda na to, że nie mogę nawet uruchomić mojego pliku wsadowego. Nie mam pomysłów.
Oto co mam w Javie:
Runtime.
getRuntime().
exec("build.bat", null, new File("."));
Wcześniej miałem Pythonowy plik Sconscript, który chciałem uruchomić, ale ponieważ to nie zadziałało, zdecydowałem, że wywołam skrypt za pomocą pliku wsadowego, ale ta metoda nie powiodła się jeszcze.
11 answers
Pliki wsadowe nie są wykonywalne. Do ich uruchomienia potrzebna jest aplikacja (np. cmd).
W Uniksie plik skryptu ma shebang (#!) na początku pliku, aby określić program, który go wykonuje. Podwójne kliknięcie w systemie Windows jest wykonywane przez Eksploratora Windows. Nic o tym nie wie.
Runtime.
getRuntime().
exec("cmd /c start \"\" build.bat");
Uwaga: za pomocą polecenia start \"\"
Zostanie otwarte osobne okno poleceń z pustym tytułem i wyświetli się tam każde wyjście z pliku wsadowego. Należy również praca z tylko ' cmd / C build.bat", w którym to przypadku wyjście może być odczytane z podrzędnego procesu w Javie, jeśli jest to pożądane.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-07 22:53:05
Czasami czas wykonywania wątku jest wyższy niż czas oczekiwania na wątek JVM, zdarza się to, gdy proces, który wywołujesz, zajmuje trochę czasu, aby go przetworzyć, użyj polecenia waitFor () w następujący sposób:
try{
Process p = Runtime.getRuntime().exec("file location here, don't forget using / instead of \\ to make it interoperable");
p.waitFor();
}catch( IOException ex ){
//Validate the case the file can't be accesed (not enought permissions)
}catch( InterruptedException ex ){
//Validate the case the process is being stopped by some external situation
}
W ten sposób JVM zatrzyma się, dopóki proces, który wywołujesz, nie zakończy się przed kontynuowaniem stosu wykonywania wątków.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-24 22:22:52
Runtime runtime = Runtime.getRuntime();
try {
Process p1 = runtime.exec("cmd /c start D:\\temp\\a.bat");
InputStream is = p1.getInputStream();
int i = 0;
while( (i = is.read() ) != -1) {
System.out.print((char)i);
}
} catch(IOException ioException) {
System.out.println(ioException.getMessage() );
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-14 16:07:32
Uruchamianie plików wsadowych za pomocą Javy, jeśli o tym mówisz...
String path="cmd /c start d:\\sample\\sample.bat";
Runtime rn=Runtime.getRuntime();
Process pr=rn.exec(path);`
To powinno wystarczyć.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-10 18:22:26
ProcessBuilder jest sposobem Java 5/6 do uruchamiania zewnętrznych procesów.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-05 18:39:00
Plik wykonywalny używany do uruchamiania skryptów wsadowych to cmd.exe
, który używa znacznika /c
do określenia nazwy pliku wsadowego do uruchomienia:
Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "build.bat"});
Teoretycznie powinieneś też być w stanie uruchomić Scons w ten sposób, choć tego nie testowałem:
Runtime.getRuntime().exec(new String[]{"scons", "-Q", "implicit-deps-changed", "build\file_load_type", "export\file_load_type"});
EDIT: Amara, mówisz, że to nie działa. Błąd, który podałeś, to błąd, który pojawi się podczas uruchamiania Javy z terminala Cygwin na pudełku Windows; czy to właśnie robisz? Problem w tym, że Windows i Cygwin mają różne ścieżki, więc Wersja Javy Dla Windows nie znajdzie pliku wykonywalnego scons na ścieżce Cygwin. Mogę wyjaśnić dalej, jeśli okaże się, że to twój problem.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-05 18:41:11
Process p = Runtime.getRuntime().exec(
new String[]{"cmd", "/C", "orgreg.bat"},
null,
new File("D://TEST//home//libs//"));
Testowane z jdk1.5 i jdk1.6
To działało dobrze dla mnie, mam nadzieję, że pomaga innym też. aby to dostać, walczyłem więcej dni. :(
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-03 22:12:47
Miałem ten sam problem. Jednak czasami CMD nie uruchamiało moich plików. Dlatego tworzę tymczasową.nietoperz na moim pulpicie, następny w tym temp.bat uruchomi mój plik, a następnie plik tymczasowy zostanie usunięty.
Wiem, że jest to większy kod, jednak działał dla mnie w 100%, gdy nawet Runtime.getRuntime ().Exec () nie powiodło się.
// creating a string for the Userprofile (either C:\Admin or whatever)
String userprofile = System.getenv("USERPROFILE");
BufferedWriter writer = null;
try {
//create a temporary file
File logFile = new File(userprofile+"\\Desktop\\temp.bat");
writer = new BufferedWriter(new FileWriter(logFile));
// Here comes the lines for the batch file!
// First line is @echo off
// Next line is the directory of our file
// Then we open our file in that directory and exit the cmd
// To seperate each line, please use \r\n
writer.write("cd %ProgramFiles(x86)%\\SOME_FOLDER \r\nstart xyz.bat \r\nexit");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// Close the writer regardless of what happens...
writer.close();
} catch (Exception e) {
}
}
// running our temp.bat file
Runtime rt = Runtime.getRuntime();
try {
Process pr = rt.exec("cmd /c start \"\" \""+userprofile+"\\Desktop\\temp.bat" );
pr.getOutputStream().close();
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
// deleting our temp file
File databl = new File(userprofile+"\\Desktop\\temp.bat");
databl.delete();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-08 21:47:35
Następujące działa dobrze:
String path="cmd /c start d:\\sample\\sample.bat";
Runtime rn=Runtime.getRuntime();
Process pr=rn.exec(path);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-11 12:54:25
Ten kod wykona dwa polecenia.nietoperze, które istnieją na ścieżce C:/folders/folder.
Runtime.getRuntime().exec("cd C:/folders/folder & call commands.bat");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-13 17:04:23
Aby rozwinąć @ Isha ' s anwser możesz po prostu wykonać następujące czynności, aby uzyskać zwrócone wyjście (post-facto nie w Rea-ltime) skryptu, który został uruchomiony:
try {
Process process = Runtime.getRuntime().exec("cmd /c start D:\\temp\\a.bat");
System.out.println(process.getText());
} catch(IOException e) {
e.printStackTrace();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-14 18:08:44