Android error- Working Directory: null Environment: null - java

I am trying executing this bat in android studio and I get the below error. I am not sure how to resolve this as I am new to android. can anyone please suggest what needs to be tried.
Process p = Runtime.getRuntime().exec("cmd /c start /src/androidTest/CopyFiles.bat");
Error:
java.io.IOException: Error running exec(). Command: [cmd, /c, start, /src/androidTest/CopyFiles.bat] Working Directory: null Environment: null
at java.lang.ProcessManager.exec(ProcessManager.java:211)
at java.lang.Runtime.exec(Runtime.java:174)
at java.lang.Runtime.exec(Runtime.java:247)
at java.lang.Runtime.exec(Runtime.java:190)

The problem is simply that you are trying to run the cmd command on a Linux (which Android actually consists of) system. cmd is the short name for cmd.exe which is located in C:\Windows\system32\cmd.exe but not on Linux/Android phones. So it does not exist. If you are only trying to copy files, you could do this with Java.
If you are trying to extend your application with plugins or extensions you may want to have a look at BeanShell. Running .bat files is not easy on Android. You would at least have to run a shell script instead. Just Google around to see how you can run shell scripts on Android and how they differ from Batch files.
Long story short: CMD is not found on your Android phone because it's a Windows program. Because it cannot be found you get this error.

Related

How to run java command as root?

I get the infamous error "Could not find or load main class..." when I am trying to run java command as root.
I tried this solution without any success.
which java returns /usr/bin/java so I tried appending at ˜/.bashrc
export PATH=$PATH:/usr/bin/java
or
export PATH=$PATH:/usr/bin, again... without success.
ps: I want to run as root because I've created a simple server using sockets and I want to ping it with an android app. The problem is that I am getting permission denied.
Maybe this could help:
sudo java -cp $CLASSPATH com.blubber.Bla

"adb shell date > date.txt" command execution not working in Mac using Process exec

I have an automation script written in java. Here is the code sample.
String command = "adb shell date > date.txt";
Process process = Runtime.getRuntime().exec(command);
When executing the code in windows its working fine but when I am running it in mac its not working properly.
Later I found that, in windows this command execution is creating file in my local PC project directory. But in case of Mac its failed when trying to create the file in device directory and error showing. While from Mac terminal its working fine.
My question is why is not working in my mac machine and why file is not creating in my local PC?
The Java Runtime environment doesn't necessarily pick up the same environment variables, command path, and aliases that are at work when you use a macOS Terminal.
You might try entering which adb into the Terminal and see what path you get back, if any. If you do get a definite path, try executing that full path, not just adb, from Java.

Java runs in CMD but not in Powershell

So, I am not new to Java and compiling in cmd but I am pretty new to using windows powershell. I have 'javac' and 'java' commands running fine in CMD and all java programs compile using netbeans and eclipse. I can also use javac inside windows powershell with no problems, but when attempting to run a java program, i.e. java MyProgram, after compiling it I get the following error...
Error: could not open 'C:\Program Files (x86)\Java\jre7\lib\jvm.cfg
Now I am running Java 8, so I have no idea why it would be looking for a file like this since CMD confirms that both 'java' and 'javac' are running version 8 and have the correct path set, or they would not run there. And please do not just tell me more about setting the paths because I have checked and checked and checked again. Also, I have made sure that anything that is related to java version 7 that I could find has been uninstalled / deleted.
Well, any advice would be appreciated. Thanks a bunch.
Try to start you application using a small cmd oder bat-File. Powershell does a lot of parsing and destroys your call. I usualy use something like this:
$Result = Invoke-Expression "cmd /c $Tempbat"
One alternative is to use the stop-parsing character: --%
icacls c:\scripts --% /grant ***\ScriptAdmin:(CI)(OI)F

Running sencha compilation without java installed causes "Could not open the pipe" error

I would like to be able to run a sencha compilation without a full java installation on my machine. I.e. I want to just include the java binaries in a tools folder. The only part of the compilation that doesn't work currently is the final part which compresses the js files. I'm getting the following error:
uncaught exception: Stream:"Could not open the pipe" (exec://java -jar
"C:\projects\Tools\SenchaSDKTools-1.2.3\jsbuilder\ycompressor\ycompressor.jar"
--type js -o "C:\inetpub\wwwroot\ext\WebApp\app-all.js" "C:\inetpub\wwwroot\ext\WebApp\app-all.jstemp-1098294810303.0708")
To try this out I created a simple js file which does only the failing step:
var cmd = 'java -jar "C:\\projects\\tools\\SenchaSDKTools-1.2.3\\jsbuilder\\ycompressor\\ycompressor.jar" --type js -o "C:\\inetpub\\wwwroot\\ext\\WebApp\\app-all.js" "C:\\inetpub\\wwwroot\\ext\\WebApp\\app-all.js-temp-1098294810303.0708"';
var stream = new Stream('exec://' + cmd);
stream.close();
then ran the simple js file using jsdb from a command line:
jsdb simple.js
When java is fully installed the above code works fine, when I uninstall java the above line fails with the error message above.
I'm guessing it's something todo with the java alias. I have little to no knowledge of this framework/language, so I'm hoping it's going to be very obvious to some experts out there as to how to fix this without a full java install. I.e. making the java alias know the location of the java binaries.
Oh dear... thanks to Jacob for helping me on this..
Add the java locations to the path environment variables and it work.
E.g.
C:\JavaLocation\jre6;C:\JavaLocation\jre6\bin

Runtime exec issues in Linux

In an application I am writing, I am launching another application (a runnable JAR) using Runtime.exec(...). Everything launches successfully in Windows, but Linux (specifically certain installations of CentOS - works in Ubuntu) has been giving me some problems. For some reason, the only way the secondary application will successfully launch is if I execute the first through a terminal. All behavior works as expected. However, if I launch the first application by double-clicking its icon (without a terminal open), the button to launch the second application seems to do nothing. I get no exceptions or error output - just a flash of my progress bar saying that it is launching, and then nothing. I can confirm through jconsole that the second application's process is never launched.
I have seen the commonly linked article on the pitfalls of the exec method ( http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html ), but have not been able to solve this problem with anything I have found there. I am in fact reading the output and error streams of the second process, as I see all output when it successfully runs (after launching the first application through a terminal command). Not knowing a lot about deeper workings of Linux, I think this sounds like it may be a permissions issue with the output stream or something, but I am not sure.
In case it helps to diagnose the problem, I am using the command:
rt.exec(new String[]{"\bin\bash", "-c", "java -jar myjarfile.jar myArg1 myArg2 ..."}); Since this works (depending on how the application is launched), I'm not too concerned that anything is wrong with this piece of code...
Anyone have any suggestions? Thanks in advance!
EDIT: The solution was to fix the directory to the JAR I was attempting to run. When launched via the GUI, user.dir was pointing to the parent directory of the folder containing my application. Since I'm using Eclipse RCP, my solution was to use
String currDirPath = Platform.getInstallLocation().getURL().toString(); instead. Thanks for the help everyone!
Since you're just using the jar file name - myjarfile.jar - and not the full path to it, depending on the current working directory, the jar may or may not be found. Try changing your exec command to use the full path to the jar instead. You can debug this by using rt.exec() to write the output of 'pwd' to a text file.
instead of
rt.exec(new String[]{"\bin\bash", "-c", "java -jar myjarfile.jar myArg1 myArg2 ..."});
use
rt.exec(new String[]{"\bin\bash", "-c", "/***path to java***/java -jar myjarfile.jar myArg1 myArg2 ..."});

Categories

Resources