Execute Shell Script using Http path in java program - java

i have a path where i have shell script which i have to execute using java program but i am getting error as .
Runtime.java
public class Runtime {
public static void main(String[] args) {
System.out.println("Triggered");
try {
Process p = Runtime.getRuntime().exec("\"http://192.168.1.7/sh_scripts/check_process/2.sh\"");
System.out.println(p);
} catch (IOException e) {
e.printStackTrace();
}
}
}
LOGCAT
http://192.168.1.7/sh_scripts/check_process/2.sh: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at com.howtodoinjava.demo.poi.Runtiime.main(Runtiime.java:15)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
Did i miss anything while executing . The path is not geeting read from the java program to execute the shell script So please help me to execute the jar file using the given http link where i can execute the 2.sh file successfully using java

As far as I know, you can only execute an executable FILE on the same computer, there is no support for downloading a script from a remote server in http, ftp or whatever else.
Same is true also in a normal unix shell.
So, you have a few options :
Download the .sh file yourself in java, put it in a temporary file, and execute it there
Run another .sh file that downloads the .sh file from http and executes it; can be a oneliner, like source <(curl -s http://192.168.1.7/sh_scripts/check_process/2.sh)
Try to execute this oneliner, maybe using ProcessBuilder can ease the creation of the redirection etc.. but I'm not sure.

Related

How to call an external application if using exec() defines it as invalid Win32 application in Java?

I'm fiddling with opening external applications from Java source code. I am trying to open a launcher for a game called Runescape, which is found inside C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Jagex. The name of the file inside this directory is RuneScape Launcher.url.
This is the code which demonstrates my progress so far:
public static void main(String[] args) throws IOException, InterruptedException {
//doesn't work
Process p = Runtime.getRuntime().exec("C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Jagex\\RuneScape Launcher.url");
//if Chrome was to be opened, it works, since it is .exe
// Process p = Runtime.getRuntime().exec("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
p.waitFor();
System.out.println(p.exitValue());
}
The error that gets thrown is:
Exception in thread "main" java.io.IOException: Cannot run program "C:\ProgramData\Microsoft\Windows\Start": CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at Main.main(Main.java:47)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
Obviously, RuneScape Launcher.url is not a valid Win32 application. How to start such an application?
My research:
- this post suggests using ShellExecute, however it is written in another programming language. I couldn't find a similar solution for Java.
- this post talks about passing parameters when calling an external application, but that external application is .exe
- this page demonstrates calling external applications, but again only .exe
Then, I've tried starting this launcher from cmd manually ... successfully. First, I've located launcher directory: cd C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Jagex and then called the launcher: "RuneScape Launcher.url". This started the launcher correctly. Why doesn't it start from Java code?
Try passing the launcher as an argument to cmd.exe:
Process p = Runtime.getRuntime().exec("cmd.exe", "/c", "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Jagex\\RuneScape Launcher.url");

issue with running git commands from java program

I am trying to run a git command through java program using
Runtime.getRuntime().exec(commandToBeExecuted);
and commandToBeExecuted ="git log" ;
i have set env variables but still getting the error like:
java.io.IOException: Cannot run program "git": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at GITCodeCoverage.createLog(GITCodeCoverage.java:40)
at GITCodeCoverage.main(GITCodeCoverage.java:17)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:188)
at java.lang.ProcessImpl.start(ProcessImpl.java:132)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
... 5 more
Any can please help me in this run this command succesfully on Windows system.
Thanks in advance!
Try using this overloaded Runtime.exec(String command, String[] envp, File dir) like:-
File gitPath = new File("path where git is located in your system");
Runtime.exec('git logs', null, gitPath);

Java error in making file through console

I want to make a file though the cmd in java using this code
Runtime.getRuntime().exec("mkdir C:\\Users\\Nick\\test");
and i get this annoying error:
Exception in thread "main" java.io.IOException: Cannot run program "mkdir": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at LFID.main(LFID.java:11)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
I have no idea what's causing it so help.
By the way please don't tell me how to create a folder not through cmd, I need to do it this way. Thanks.
mkdir isn't a standalone executable you can launch as a separate process - it's a command that the Windows command shell understands.
So you could run cmd.exe /c mkdir ...:
Runtime.getRuntime().exec("cmd.exe /c mkdir c:\\Users\\Nick\\test");
Or:
Runtime.getRuntime().exec(
new String[] { "cmd.exe", "/c" "mkdir" "c:\\Users\\Nick\\test"});
... but I'd still recommend just using File.mkdir instead... why call out to an external process when you can do it within Java? (If you're going to specify an odd requirement, it helps to give some more context on it...)

Run Bash Command from JAVA on AWS EC2 Instance

I am new to using AWS SDK on Java. I have created a program to create AWS instances from Java and to check if its running. But I am not able to figure out how to run a bash command on that running instance. Please help!
try
{
String lscmd = "ls";
Process p=Runtime.getRuntime().exec(lscmd);
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while(line!=null)
{
System.out.println(line);
line=reader.readLine();
}
}
catch(IOException e1) {
e1.printStackTrace();
}
catch(InterruptedException e2) {
System.out.println("Pblm found2.");
}
But I get an error saying,
java.io.IOException: Cannot run program "ls": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at ListInstances.main(ListInstances.java:79)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
I am not sure how to connect to the instance I created and run this command on that instance.
Could it be that the PATH is not defined and it doesn't know where to find ls? Try with the full path (e.g. /bin/ls) to verify.

XMLBeans: java.io.IOException: Cannot run program "C:\xmlbeans\bin\jar":

I'm trying to run XMLBeans:
scomp -compiler "C:\Program Files (x86)\Java\jdk1.6.0_37\bin\javac.exe" -cp "C:\Components\*";"C:\Components\jsr173_api.jar";"C:\xmlbeans\lib\xbean.jar";"C:\xmlbeans\lib\xbean_xpath.jar";"C:\xmlbeans\lib\jaxen-1.1-beta-2.jar;" -out S2002PDPIn.jar S2002PDPIn.xsd
It gets an error below:
java.io.IOException: Cannot run program "C:\xmlbeans\bin\jar":
CreateProcess error=2,
The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at org.apache.xmlbeans.impl.tool.CodeGenUtil.externalJar(CodeGenUtil.java:304)
at org.apache.xmlbeans.impl.tool.SchemaCompiler.compile(SchemaCompiler.java:841)
at org.apache.xmlbeans.impl.tool.SchemaCompiler.main(SchemaCompiler.java:272)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 6 more
BUILD FAILED
At first I thought the "C:\xmlbeans\bin\jar" it was looking for was a directory so I just created a folder "jar", but when I run it again the "jar" was referred as a file where I get error:
java.io.IOException: Cannot run program "C:\xmlbeans\bin\jar":
CreateProcess error=5, Access is denied
Please kindly help me how to fix this so that it don't look for "jar" file.
I looked "scomp.cmd" but there is no line that will do this.
Thank you in advance.
You are simply calling more applications, than you expect:
scomp -compiler "C:\Program Files (x86)\Java\jdk1.6.0_37\bin\javac.exe" -cp "C:\Components\*";
"C:\Components\jsr173_api.jar";
"C:\xmlbeans\lib\xbean.jar";
"C:\xmlbeans\lib\xbean_xpath.jar";
"C:\xmlbeans\lib\jaxen-1.1-beta-2.jar;" -out S2002PDPIn.jar S2002PDPIn.xsd
This happens because of ";", you would have to enclose the complete classpath with one " and seperate the values by ;.

Categories

Resources