I have created a jar file called test.jar under C:\jars. I have JAR file under the same location named run.bat and it contains the below code -
#echo off
set exec_path=C:\jars java -cp %exec_path%/test.jar; com.mycomp.myapp.MyProgram "%1"%*
#echo on
It is running successfully from command prompt with parameters.
Now I would like to run it from another JAVA program.
Please suggest.
Thanks!
I've encountered this issue before. The answer is that you have to run cmd.exe or bash or whatever shell you've got, then feed in the command to that process via the process input/output streams.
Process p = Runtime.getRuntime().exec("cmd");
p.getOutputStream().write("mybatch.bat\n");
Related
I'm rank new to bash thus the question.
I've a java program that I've exported as a .jar.
This file when run as
java -jar somefile.jar
goes into an infinite loop and awaits a file name. Based on the correct file path it generates an output.
How do I write a bash script to do automated testing of this project.
I need the scrip to do the following -
Run the program, which is run the same command
provide an array of 5 files as an input to the program
For each file write the output to an log file.
This should do it.
#!/bin/bash
files="$#"
for i in $files;
do
echo "Doing $i"
java -jar somefile.jar <<< "$i"
done
Make sure you chmod u+x filename it first. Then call it like this:
./filename firstfile secondfile thirdfile etc.
Other:
As sjsam pointed out, the use of <<< is a strictly bash thing. You are apparently using bash ("I'm rank new to bash..."), but if you weren't, this would not work.
Suppose my java program is HelloWorld.java. We can run it in 2 ways:
1st using executable jar
2nd by running java class from terminal
create a new text file and name it hello.sh
In hello.sh
!/bin/bash
clear
java -jar HelloWorld.jar
Save it and open terminal:
1 navigate to directory where your HelloWorld.jar is present
2 give permission to terminal to run the bash script by using the following command
sudo chmod 754 hello.sh
3 run you script by running the following command
./hello.sh
I m using Linux.
I want to call a small executable application from my java command line which is called "wmic". It needs an input query. Output are stored in text file in the specific directory.
When I use the command in Linux Terminal
echo "Hello World" >> /home/kannan/hello.txt
the output is stored in hello.txt file.
but when i call this command from java
Process p = Runtime.getRuntime().exec("echo \"Hello World\" >> /home/kannan/hello1.txt");
the output is not created any hello1.txt file
Please any one help me.
Thanks in Advance.
Use a ProcessBuilder. It makes it easy to redirect output of a command to file as shown below:
new ProcessBuilder("echo", "hello").redirectOutput(new File("output.txt")).start();
If you want to append to the output file:
new ProcessBuilder("echo", "hello").redirectOutput(Redirect.appendTo(new File("output.txt"))).start();
What you are executing is bash command (echo). Your java program do not work as bash interpreter
To execute any script which requires bash or shell scripting features, your need to execute that interpreter
To solve your problem you can follow below steps
1. Write your string into temp .sh file. Lets call it temp.sh
2. execute below using Runtime.getRuntime().exec
Process p = Runtime.getRuntime().exec("bash temp.sh");
bash will try to execute any command in temp.sh
I have to create a Unix shell script to execute Java code which takes a input file and creates an output file. This is how I do it on Windows:
C:\Work\MCDExcelParserJE\bin>java -classpath .\;.\jxl.jar medicaid.Test PROCESS
How can I run this on Unix?
First thing is that you need to use colon(:) instead of semi-colon(;) in your classapth:
java -classpath .\;.\jxl.jar medicaid.Test PROCESS
should be updated to
java -classpath ./:./jxl.jar medicaid.Test PROCESS
Make sure you have jdk/bin directory is added to path in order to run the java commands.
I am trying to execute ls command through busybox.
I am creating a .bat file to execute this command which i am calling through .java
However, i am not able to execute commands one after another in .bat file.
This is the contents of my .bat file
"C:\Documents and Settings\Some Directory\Android\android-sdk\platform-tools\adb.exe" shell
/data/busybox/busybox ls
what i think that once i start the shell through the first line of my .bat, the control from the shell is lost hence second command is not executed.
Because if i write my .bat file as
"C:\Documents and Settings\Some Directory\Android\android-sdk\platform-tools\adb.exe" shell ls
it works fine.
I need to write commands in my .bat file so that they exceute one after the another.
I have tried using CALL before each commands in .bat, still it does not work.
I have tried to use multiple .bat, still a fail cause.
Can someone please help me on this?
Thanks a ton.
I am unable to test this myself with ADB at this moment, but this works for other programs that have an input buffer. I will try to verify this tonight, but if anyone else confirms it before then, leave a comment.
#echo off
( echo shell
echo /data/busybox/busybox ls
) | adb.exe
IS there any way to start a blank console in Windows platform ?
I'm writing a CLI where i want to open a separate window where user can log in and write their own command. When executing with cmd /c start command, it starts windows standard console.
Is there any other command ???
Assuming you're trying to start a java jar file, use a command like this:
start /d "%~dp0" java -jar "%~dp0\fpa.jar"
%~dp0 expands to the drive letter and path in which the batch file is located. Use that if you want to want to make sure that the PWD when running is the same location as the batch file. Otherwise, juse use
start java -jar "%~dp0\fpa.jar"
This will make sure that the batch file works even if you run it when not in the same directory as the jar file, as long as the jar file is in the same directory as the batch file.
You may need to make sure that java is in your path by having a line like
set path=jre6\bin;%PATH%
Also, you can eliminate the command line windows that comes up (for a GUI program by example) by using javaw instead of java.
You can use batch file in windows which would in turn start application that would accept user input. Something similar to this :
start cmd /c myapp.bat