java.lang.Runtime exception Cannot run program
Runtime.exec does not use a shell (like, say, /bin/bash
); it passes the command directly to the operating system. This means wildcards like *
and pipes (|
) will not be understood, since cat
(like all Unix commands) does not do any parsing of those characters. You need to use something like
p = new ProcessBuilder("bash", "-c", command).start();
or, if for some bizarre reason you need to stick to using the obsolete Runtime.exec methods:
p = Runtime.getRuntime().exec(new String[] { "bash", "-c", command });
If you are only running that cat/grep command, you should consider abandoning the use of an external process, since Java code can easily traverse a directory, read lines from each file, and match them against a regular expression:
Pattern pattern = Pattern.compile("TEXT_TO_SEARCH");
Charset charset = Charset.defaultCharset();
long count = 0;
try (DirectoryStream<Path> dir =
Files.newDirectoryStream(Paths.get("/home/talha"))) {
for (Path file : dir) {
count += Files.lines(file, charset).filter(pattern.asPredicate()).count();
}
}
Update: To recursively read all files in a tree, use Files.walk:
try (Stream<Path> tree =
Files.walk(Paths.get("/home/talha")).filter(Files::isReadable)) {
Iterator<Path> i = tree.iterator();
while (i.hasNext()) {
Path file = i.next();
try (Stream<String> lines = Files.lines(file, charset)) {
count += lines.filter(pattern.asPredicate()).count();
}
};
}
Cannot run bat file in java
Your can try this
Runtime.getRuntime().exec("cmd /c start run.bat", null, new File("C:\\Test\\"));
java.io.IOException: Cannot run program dir : CreateProcess error=2, Das System
Add "cmd.exe /c"
at the beginning of your command string, that should do the trick.
The /c
parameter will make the cmd
finish and return it to the Java process.
Without it, the process will hang.
Cannot run program ... : CreateProcess error=2
cd
is not a program that you can execute. Even if you could it would do nothing.
When you exec
, a new process is started. This new process is independent of your process (the Java process), and has it's own "current directory". Changing the current directory in that process will not affect the current directory of the Java process.
cd
is a built-in command of the command-line program cmd.exe
. To run a cd
command, you need to run cmd.exe /c cd ...
. But as I just stated above, it would be meaningless (the process would end immediately).
As for changing the current directory of the Java process, see this: Changing the current working directory in Java?
W/System.err: java.io.IOException: Cannot run program python : error=13, Permission denied
I think the way, you run Python in android is wrong. there are some ways you can use it.
Chaquopy
a plugin for Android Studio’s Gradle-based build system.Chaquopy enables you to freely intermix Java and Python in your app, using whichever language is best for your needs
Kivy
a cross-platform OpenGL-based user interface toolkit. You can run Kivy applications on Android, on (more or less) any device with OpenGL ES 2.0 (Android 2.2 minimum).Kivy APKs are normal Android apps that you can distribute like any other, including on stores like the Play store
Pyqtdeploy
a tool for deploying PyQt applications. It supports deployment to desktop platforms (Linux, Windows and OS X) and to mobile platforms (iOS and Android).
Termux
an Android terminal emulator and Linux environment app that works directly with no rooting or setup required. A minimal base system is installed automatically
for details, you can see this wiki chart
Related Topics
Java Jsch Changing User on Remote MAChine and Execute Command
Tomcat 7: How to Set Initial Heap Size Correctly
Difference Between Using Java.Library.Path and Ld_Library_Path
How to Get the Ip of the Computer on Linux Through Java
How to Kill a Linux Process in Java with Sigkill Process.Destroy() Does Sigterm
Cannot Load R Xlsx Package on MAC Os 10.11
Wait Until Tomcat Finishes Starting Up
What Would Cause a Java Process to Greatly Exceed the Xmx or Xss Limit
Why Does Reading from Process' Inputstream Block Altough Data Is Available
Javafx: Could Not Find or Load Main Class Only on Linux
How to Open a Unc Path from Linux in Java
Kiosk Mode for Linux Java Swing Application
How to Set Ld_Library_Path for Java Process
Java -Xbootclass Path Is No Longer a Supported Option