How to run Windows command from Java - java

I would like execute the following command with Java:
"C:\Program Files\MySQL\MySQL Server 5.5\bin\"mysqldump -u root
--password=xxxx --routines database > C:\Users\john\Desktop\backup.sql
The command works perfectly when I use the Windows cmd.exe but not with my Java application.
cmd = "\"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\\"mysqldump
-u root --password=xxxx --routines database > C:\\Users\\john\\Desktop\\backup.sql
Runtime runtime = Runtime.getRuntime();
process = runtime.exec(cmd);
I got the following error:
"C:\Program Files\MySQL\MySQL Server 5.5\bin\"mysqldump -u root
--password=xxxx --routines database > C:\Users\john\Desktop\backup.sql java.io.IOException: Cannot run program ""C:\Program": CreateProcess
error=5, Access is denied
Do you have any idea?
Thank you.

You're trying to use a redirect (>) which is a function of the shell.
You don't have a shell, you're exec'ing the mysqldump process directly.
You either need to read the output stream from the process object and write it to a file yourself, or exec the windows shell to execute mysqldump for you along with the redirect.

Error 5 is either 1) cannot access the file/folder or 2) don't have permissions.
Looks like the space in your 'Program File' directory name may be causing trouble causing trouble. Check where it says "Cannot run program ""C:\Program". This is obviously not a reference to the command you are trying to run. What I would do is add MySQL to your path and then just call the mysqldump command without the path to it. If you don't want to do this, you can define a system variable and reference it with %MYSQL_HOME%\mysqldump.
Once you have the path resolved, if this still doesn't work, you'll want to look at the permissions for the command. You'll need to make sure that the process has permission to execute the command. I think this in local admin groups or something like that.
Dollars to donuts its your path tho.

Related

Bash, Windows 10, Java - No such file or directory for Java application

I'm trying to execute a Java file I was given on Windows 10, inside of the Bash shell.
I open my command prompt. I enter bash.
I set
JAVA_CALL="C:/Program Files/Java/jdk1.8.0_192/jre/bin/java"
I try to execute the call, but to no luck. I read several threads on here and tried several things. I made sure my path includes both the Program Files x86 and the regular Program Files version of my JAVA.
I executed
sudo ln -s -f /mnt/c/Program\ Files/Java/jre1.8.0_192/jre/bin/java.exe /bin/java
To try and make a link to it. I cannot get it to wrong. It always tells me
-bash: C:/Program Files/Java/jdk1.8.0_192/jre/bin/java: No such file or directory
I am sure that file exists. Any ideas?

unable to launch HBase shell

I am able to get localhost:16010 running. But, somehow the Hbase shell is not launching when I use :
01HW993798:bin tcssig$ cd /Users/tcssig/Downloads/hbase-1.0.3/bin
01HW993798:bin tcssig$ hbase shell
-bash: hbase: command not found
When I directly launch Hbase Unix executable, it generates the below error log.
Error: JAVA_HOME is not set
Although I have set it. After this only, the localhost:16010 is running.
NOTE : I know there is one similar question, but no relevant answers are present there.
Using this I am able to invoke the command, but now it gives the error :
./hbase: line 403: /Users/tcssig/Downloads/hbase-
1.0.3/bin/JAVA_HOME:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Cont``ents/Home/bin/java: No such file or directory
Although I have java file there.
Your hbase invocation should be like this:
cd /Users/tcssig/Downloads/hbase-1.0.3/bin
./hbase shell [Note the ./]
When you just type hbase shell linux searches for hbase executable in all directories included in PATH environment variable. Since above bin directory is not included it errors out.
Alternatively you can also update your path variable, based on linux distribution, the command to do that may vary. It should be something like:
export PATH=/Users/tcssig/Downloads/hbase-1.0.3/bin:$PATH
Put this command in your .bashrc or .bash_profile and then source this file. That way the bin directory is now included in PATH and hbase command is available.
Go into $HBASE_HOME/bin path, and try:
./hbase shell

How to run azure-cli commands from java

I have azure-cli installed in my mac and I can successfully execute all azure commands from cli. I want to do the same from a java application. I have seen examples of using REST endpoints and azure-sdk-for-java. But what I want is to execute the azure-cli commands directly from java.
I tried running 'azure login' from a java program, but I got the following error.
Cannot run program "azure": error=2, No such file or directory
The problem is in a lack of your PATH variable. If you want to enjoy the same "find the command" capabilities that you have on the command line, you need to set it somehow within your Java program: import the PATH variable from the environment, set it within the code you're writing, or whatever mixture covers your needs.
Giving the absolute (full) path is the surest way, but you may not want to read such long command names in your source codes.
#harshithabt Per my experience, the issue was caused by the command azure could not be searched in the directories listed in PATH. There are two ways to solve the issue for running commands in Java, please see below.
Setting up the environment variable PATH for the current shell session or the configuration files ~/.profile(or ~/.bashrc).
If you command export PATH=<your-node-path>/bin:$PATH in a shell session, you only run the Java program with azure command in the current shell session, even run via the Java IDE which must be opened in the current shell session (it means you should open Eclipse via command <your-ide-path>/eclipse).
If you configure the files ~/.profile or ~/bashrc to add the node runtime as below, please make sure the configuration files have been reloaded via command source ~/.profile in a current session or restart sessions via logout & login or reboot.
A simple way is that adding the command links into the dirs listed in PATH, such as /bin. You just need to run the commands below.
sudo ln -s /bin/node /bin/node
sudo ln -s /bin/azure /bin/azure
Then you can run azure-cli command from Java successfully.

How to execute my own commands on terminal from java File

I am trying to make a eclipse project in Java to launch commands with some buttons. The libraries of Ros fuerte (These ones i want to use) are correctly installed and concretly i am trying to launch a ros command from a Java File using:
String cmd = "roscore";
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmd);
If i launch this command from a current terminal it works, but if i do it from the java file i have a problem because the terminal doesnt recognize the command.
java.io.IOException: Cannot run program "roscore": java.io.IOException: error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:475)
at java.lang.Runtime.exec(Runtime.java:610)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at LaunchTerminal.main(LaunchTerminal.java:24)
I think that i need to add some path or similar but i dont find the information. Does anybody know how to do it?
Thank u.
only normal commands are possible to execute like rm or cd ... al others must be referenced with full path of context
Do the following if you are using the groovy distribution:
String cmd = "source /opt/ros/groovy/setup.bash && roscore";

Executing cmd.exe commands from Java

I'm trying to read a file from the user, in which each line is a cmd.exe command, and run it (it's okay to assume the commands are legal), but when I give a command like echo hi, I get runtime exception error:
Exception in thread "main" java.io.IOException: Cannot run program "echo": CreateProcess error=2, The system cannot find the file specified
I'm trying to run the commands like this:
Runtime.getRuntime().exec(command);
where command = "echo hi". This does work for commands like regedit though, so it seems the runtime I'm getting is like the "run" window and not cmd. Is there a way to run these commands?
That's because echo is not an external executable command (i.e., there is no echo.exe file on your hard disk, unless you put it there yourself). It's an internal command of the shell.
You'll probably find that you need to execute something like:
cmd.exe /c echo hello

Categories

Resources