Java.Lang.Runtime Exception "Cannot Run Program"

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



Leave a reply



Submit