cmd windows path and environment variables unrecognized in Java Command Prompt - java

I am trying to execute the following
on the command line via the java runtime environment.
Runtime rt = Runtime.getRuntime();
String runtime = "cmd /c start cmd.exe /k \"cd /d C:\\Users\\User\\Documents\\ & python book.py \" "
rt.exec(runtime);
When running the command prompt directly, i.e. python book.py (assuming that I have already changed the location to the correct directory), python runs fine without any issues.
However, when done through java, the command prompt window looks different,
with C:\WINDOWS\system32\cmd.exe instead of displaying Command Prompt.
The above java runtime also gives me 'python' is not recognized as an internal or external command, operable program or batch file. (Whereas the normal command prompt the runs python perfectly fine)
How would I go about including my path and environment variables such that python, or any other path/environment variable, is recognized when I run the command prompt from java?

This may sound naiive but apparently, the solution was to restart the computer. I guess the PATH variables in the command prompt that was run JAVA weren't synchronized like in the other instances of running the command prompt directly.

Related

Command prompt do not close after running batch file which have 'exit' in last line

I am running a batch (ScanProject.bat) file using java by following code
Process p= Runtime.getRuntime().exec("cmd /c start /wait ScanProject.bat "+ BaseProjDir+"\\"+jo.getString("Name")+" "+st.nextToken());
System.out.println("Exit value : "+p.waitFor());
And following is the batch file code :-
%2:
cd %1
ant -f ..\antbuild.xml analyse
exit
Batch file run successfully but problem is command prompt window do not closes automatically and hence Process do not terminated automatically and my program wait for infinite time to complete this process.Please suggest me any technique so that cmd exit after running ant -f ..\antbuild.xml analyse command.
Thanks.
cd /D "Full path of directory" or pushd "Full path of directory" with popd before exit is better to switch the current directory to any directory on any drive (cd and pushd/popd) or even to a network share (just pushd/popd). Run in a command prompt window cd /? and pushd /? for details.
cmd /C starts a new Windows command process with closing the process automatically after last command was executed. Run in a command prompt window cmd /? for details on options of Windows command interpreter.
start is a command to start a new Windows command process or a GUI/hybrid application in a separate process.
So what you do here is starting a new Windows command process which starts a new Windows command process.
Running in a command prompt window start /? outputs the help for this command. start interprets often the first double quoted string as title string for the new command process. This causes often troubles on command lines with at least 1 double quoted string. Therefore usage of start requires often an explicit definition of a title string in double quotes as first argument for start which can be even an empty string, i.e. simply "" as first argument after start.
As it can be read after running exit /? in a command prompt window, this command without /B always exits the current Windows command process immediately. So when ant.exe finished, the command process in which the batch file was processed is definitely terminated.
I'm having no experience on Java development, but in my point of view it should be enough to use the following execution command which does not need a batch file at all.
The Java code line
Process p= Runtime.getRuntime().exec("cmd.exe /C cd /D \"" + jo.getString("Name") + "\" && ant.exe -f ..\\antbuild.xml analyse");
should be enough to
start a new Windows command process,
set the current directory within this command process to the drive and directory specified by jo.getString("Name") which of course must return a directory path with drive letter and using backslashes as directory separators, and on success
execute ant in this directory with the specified parameters
with terminating the Windows command process automatically after console application ant.exe finished if ant.exe is a console application.
I'm not sure if cmd.exe /C is needed at all.
I suggest to test this command first manually from within a command prompt window. Then use it in the Java application if really working and producing the expected result. And finally I would further test if cmd.exe /C is needed at all in Java code.
See Single line with multiple commands using Windows batch file for details about the operator && to run a command after previous command was successful. And see also Why do not all started applications save the wanted information in the text files as expected? for an explanation of console / GUI / hybrid application.
NOTE: There is also Java Runtime method exec(String[] cmdarray, String[] envp, File dir) to execute a command like ant.exe with its parameters -f and ..\antbuild.xml and analyse in the directory defined with third parameter which might be better for this task.
Swap out exit for taskkill, assuming you do not have any other cmd processes running. Not very graceful but it will get the job done.
%2:
cd %1
ant -f ..\antbuild.xml analyse
taskkill /im cmd.exe

Cannot compile java file using javac command under JDK on ubuntu (APARAPI)

I have run the same command under windows using the windows Java Development Kit and it worked.Now i try to run it ubuntu and i get this error.Please help
Different OS have different dist package manager, you try to install Java environment in your Linux system by below Reference
It looks like you are specifying the -cp argument incorrectly.
On Linux, the path separator character is ":" not ";". The ";" character separates commands on the command line, assuming that you are using bash or similar as your shell.
It looks like your command has been interpreted as two commands:
$ javac -g -cp ../dist_windows_x86_64/something
$ . GPUGalaxySim.java
The javac command fails because there are no source filenames on that command line ... just like the error message says.
Then the second command:
The dot command (".") is a built-in shell command that "sources" a file and attempts to interpret it as shell commands.
If you attempt to "source" a file that is actually Java source code, you get nonsense error messages, basically because the shell has no clue what Java code means.

How to add the environment variables to the android Runtime.getRuntime().exec command?

There's an issue when I run the android Runtime.getRuntime().exec method.
I have a native binary file to be run on android and I start it with the java method Runtime.getRuntime().exec. But running this native binary file requires the addition of an environment variable. So I execute this command:
envSetCmd = {"sh", "-c", "export LD_LIBRARY_PATH="+excBinFilepath+":$LD_LIBRARY_PATH"}.
It doesn't work when I check the environment variable with the command:
sh, -c, echo $LD_LIBRARY_PATH.
I think the reason is that when I set the environment variables I start a shell and when I check it with the command "echo" another shell was started. So the environment variables didn't work in the shell I check it.
I think there are two ways to solve this issue. The one is that running two commands in one shell. So I tried to use the command:
{"sh", "-c", "export LD_LIBRARY_PATH="+excBinFilepath+":$LD_LIBRARY_PATH", "-c", "echo $LD_LIBRARY_PATH"}.
Unfortunately it is illegal. The other is that I add the environment variables to the android user startup files. I tried to echo the $PATH, and I see the /system/bin/ and other path was loaded at the startup of android.
So I think there must be a file just like the ~/.bashrc in linux which could set the users' environment.
Any one could give me some clues about the ways I listed above? By the way, I shouldn't root the phone!
try to execute whatever you need to execute with env:
{"env", "LD_LIBRARY_PATH="+excBinFilepath, "yourCmd"}

Able to run programme in command promt but not through PHP system/exec command

I can run one program in my shell (Linux), but executing the same command on the same PC with PHP (using system or exec commands) does not work.
I can execute simple commands using system command in PHP (like ls or which Java).
I changed the permission of particular folder/program, and used whole path for my files and program.
system("/usr/bin/java -jar /pathforprogram/programe.jar -convert /pathtforinput/inputfile.txt -xtg outputfile.txt >optional.txt");
What can be the reason for this?

java runtime cmd date & time commands

I managed to change system time and date using runtime in java. However I have to run this two commands one at a time opening two command prompts instead of one because if run both commands at the same time the command prompt gets them as one invalid command
//this is the working code that opens two cmd`s:
Runtime rt = Runtime.getRuntime();
rt.exec("elevate.cmd cmd.exe /c time 11:30");
rt.exec("elevate.cmd cmd.exe /c date 02-04-2012");
//this is the code that I think it should open one cmd and execute both of the commands
Runtime rt = Runtime.getRuntime();
rt.exec("elevate.cmd cmd.exe /C time 11:25 /C date 02-05-2012");
But the cmd is returning "The system cannot accept the time entered".
Note: the elevate.cmd is a batch file I use it to run the cmd as administrator(win7) and you can download it from here.
How can I make the system change both time and date by opening cmd once? Or what other choices do I have?
Try solutions from this SOq:
How to execute cmd commands via Java
Basically, create a process and then "write" commands to it as if you were an user typing them. I don't have Win7 to test (don't know how it will behave in combination with elevated privilege prompts), but it works for me on Ubuntu 11.10 - hope it works for your case, too.
Alternatively, you can make another .cmd file (called e.g. changedt.cmd) that will contain the two commands:
elevate.cmd cmd.exe /c time 11:30
elevate.cmd cmd.exe /c date 02-04-2012
and then run it instead:
Runtime rt = Runtime.getRuntime();
rt.exec("changedt.cmd");
This should work in any case, as you seem to be successfully running pretty much the same thing, but adds another .cmd file.
Hope this helps.

Categories