Using 'cmd /c dir' vs 'dir' - java

Using the method
public Process exec(String command, String[] envp, File dir) throws IOException
I am supposed to print the output of the command dir [filepath] but using Java,
and my question is, why do I have to type for the command argument, (cmd /c dir), and not just (dir)?
If I type dir [filepath] in command prompt for Windows, it works, but not in Java.

exec() executes new process, and for that you need executable file to run (i.e. some .exe file, if we talk about Windows). But there is no "dir.exe". "dir" is built-in command of the Windows Command Prompt, cmd.exe, so to run it, you need to run cmd.exe and pass such command to it.
In contrast, for example, in Linux, ls is separate executable, so there you can directly do something like ls /home/myuser instead of sh -c 'ls /home/myuser'.

Related

Execute a .jar file using shell script

I want to execute a jar file of DOMO CLI from a shell script. The jar file itself has some functions which I want to call after I call the main jar file. The problem which I am facing is that after it executes the jar file, I am not able to pass the additional commands to execute inside that jar through a shell script. It just stops after calling jar and doesn't take further commands. Can anyone please help? Below is the code I am calling from a shell script.
java -jar XX.jar
The commands are as below which follow the above jar. So once we enter into the above jar we have to execute the below commands one after the other. I am not sure how to achieve this through a shell script.
connect -s X.domo.com -t Ysssss
upload-dataset -a -i dhdhdhdh -f /prehdfs/dev/comres/herm/data/yyyy.csv
Did you try using pipes and inputs.
When you execute above it runs it under a child shell.
You may try below format if not tried already
$ (echo "connect -s X.domo.com -t Ysssss" && cat) | java -jar XX.jar
If you can reference a file in your use case, you could put your commands in a file.
File: list_my_datasets.domo
connect -t ... -s ...
list-dataset
quit
then run the command:
java -jar domoUtil.jar -script list_my_datasets.domo > datasets
I wanted the data from it so I piped to a file (where I had to grep what I wanted), but you would omit that I believe, unless it has some output you'd want to check. I haven't tested with the upload command, but I would hope any commands substituted or added to the example work similarly.
Domo docs on scripting

How to make the command line invisible after launching the jar software using bat file?

I have a bat file which launches a jar file.
path\to\java -jar software.jar
This command launches the command line in the background along with the software. I tried start "ABCD" cmd /k path\to\java -jar software.jar and start "ABCD" cmd /c path\to\java -jar software.jarand both of them didn't work.
How to make the command line invisible after the launch of the software?
some day ago i also face this type of problem ....
but in my case i run jar file manually from CMD using
java -jar test.jar
after running this command when i close cmd jar file stop working ... so i use a trick .. to run jar file as windows service ....using this command
javaw -jar test.jar
after running this command i close my cmd safely..
so you can try this
Replace java with javaw so your java application runs detached from the console window.
Then place an exit into your batch file, this may end the cmd that opened the window.

Java runtime exec zip command with "*" character

I want to zip contents of folder without including folder it self using Java Runtime
For example: zip folder /home/duyvt/example
In terminal, I exec the following commands and it works well. Archive.zip will include only contents of example folder.
$ cd /home/duyvt/example.
$ zip -r /home/duyvt/archive.zip *
But in Java Runtime, it does not work.
Runtime.getRuntime().exec("zip -r /home/duyvt/archive.zip *", null, new File("/home/duyvt/example"));
It seems Java Runtime does not understand "*" character.
I have also tried to use "ProcessBuilder" but not work too.
Is there anyone can help me ?.
Thanks for any help.
On Linux, it is the shell that understands and expands *.
On Windows, the command program has to do it itself.
Java is generic, and does not implement shell functionality.
If you want shell functionality, run the command through the shell, e.g.
sh -c 'zip -r /home/duyvt/archive.zip *'
UPDATE
From javadoc of exec(String command, String[] envp, File dir):
This is a convenience method. An invocation of the form exec(command, envp, dir) behaves in exactly the same way as the invocation exec(cmdarray, envp, dir), where cmdarray is an array of all the tokens in command.
More precisely, the command string is broken into tokens using a StringTokenizer created by the call new StringTokenizer(command) with no further modification of the character categories. The tokens produced by the tokenizer are then placed in the new string array cmdarray, in the same order.
Since StringTokenizer doesn't understand the shell quoting using '', the command is split incorrectly, and you'll have to do it yourself:
Runtime.getRuntime().exec(new String[] { "sh",
"-c",
"zip -r /home/duyvt/archive.zip *" },
null, new File("/home/duyvt/example"));
Start shell and pass it a command, like so:
"sh -c 'zip -r /home/duyvt/archive.zip *'"

ArrayIndexOutOfBounds Exception on executing linux sh file

I have a program in java which takes 0'th aargument as file location like
File f = new File(args[0]);
so when i execute it using a windows batch(.bat) file it works correctly .
but when i execute the same using a linux shell file(.sh) in linux i get ArrayIndexOutOfBoundsException.
WINDOWS BATCH FILE :
#echo off
for /f %%i in ("%0") do set scriptpath=%%~dpi
set cp=%scriptpath%/../lib/*.jar;
java -classpath %cp% com.synchronizer.main.MYSynchronizer %scriptpath% "%1" "%2"
LINUX SH FILE:
export JAVA_HOME=/usr/local/java
PATH=/usr/local/java/bin:${PATH}
THE_CLASSPATH=
for i in `ls ../lib/*.jar`
do
THE_CLASSPATH=${THE_CLASSPATH}:${i}
done
java -cp ".:${THE_CLASSPATH}" \
com.synchronizer.main.MYSynchronizer
please help!
It looks like a problem in script (no arguments are passed to the Java program).
You can consider to debug the script like this: debugging scripts
Hope this helps
Your shell script is not passing any parameters:
java -cp ".:${THE_CLASSPATH}" com.synchronizer.main.MYSynchronizer
Try:
java -cp ".:${THE_CLASSPATH}" com.synchronizer.main.MYSynchronizer "$1" "$2"
As stated above, your Linux shell script is not sending any arguments to the Java program that you are trying to start.
And, adding to that, you are not showing us how you run the Linux shell script. If no argument is given on the command line when you start the shell script, no arguments can be passed to your Java application from the shell script.
If you want to see the actual command that is going to be run by your shell script, you can always put "echo" in front of a line and see what all variables are expanded to. This is a simple way to debug shell scripts.

how to launch java GUI test from linux

-java -classpath<> <classname> in the ".bat" file to launch java test from cmd windows
how to do that using perl to launch java test from linux ?
Don't use perl. For such a simple job, a simple shell script will do:
#!/bin/sh
/path/to/java -classpath foo.jar:bar.jar:. classname
Make the file executable with chmod +x filename and execute it with ./filename
A similar approach using the -jar option is possible. Additionally, you can forward any command line parameters using the special parameter #.
#!/bin/sh
/path/to/java -jar foo.jar "${#}"

Categories