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.
Related
I need to run a java script in a java docker container. This java code uses ProcessBuilder to run a command. When executing the container I get the following error:
Exception in thread "main" java.io.IOException: Cannot run program "cmd.exe": error=2,
No such file or directory
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1143)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
at test.main(test.java:39)
Caused by: java.io.IOException: error=2, No such file or directory
at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:314)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:244)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1110)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
at test.main(test.java:39)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at jdk.compiler/com.sun.tools.javac.launcher.Main.execute(Main.java:419)
at jdk.compiler/com.sun.tools.javac.launcher.Main.run(Main.java:192)
at jdk.compiler/com.sun.tools.javac.launcher.Main.main(Main.java:132)
The code producing the error is the following:
String [] test = {"cmd.exe", "/c", "cd \"/d D:\\4.12.0.0\\my_Software\\Startscripts\\Windows \" && pscconverter single C:\\Users\\Kuehnel\\Documents\\Projekte\\S2_Deployment_PipeLine\\Test_cmd_line D:\\target"};
ProcessBuilder builder = new ProcessBuilder(test);
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
System.out.println(line);
if (line == null) {break;}
}
I use a custom tool that converts the content of a folder into a psc file. For this I first access the folder where the pscconverter.cmd file is located and then I execute it with the required input parameters. First the mode "single" following two paths for input and output folder.
Is it possible to access runtime in a Docker container?
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...)
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.
I'm trying to use processbuilder to spawn a new JVM in eclipse with java 7 u51. Unfortunately, I am having a problem with the path ( String path = System.getProperty("java.home"); ) when the processBuilder attempts to use it.
Here is the problem code
String separator = System.getProperty("file.separator");
String classpath = System.getProperty("java.class.path");
String path = System.getProperty("java.home");
System.out.println("Seperator = " + separator + " classpath = " + classpath + " path = " + path);
ProcessBuilder processBuilder = new ProcessBuilder(path, "-cp",
classpath, Transcriber.class.getName());
Process process = processBuilder.start();
Here is a look at the console output
java.io.IOException: Cannot run program "C:\Program Files\Java\jre7": CreateProcess error=5, Access is denied
Unable to call transcribeConvo
at java.lang.ProcessBuilder.start(Unknown Source)
at TranscribePanel$2.run(TranscribePanel.java:131)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: CreateProcess error=5, Access is denied
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 7 more
I tried adding the jvm.dll directly to the eclipse.ini but it didn't have an affect. Any help would be great. Thank you :)
Ps: If I didn't provide enough information please let me know.
The path "C:\Program Files\Java\jre7" is not the path to an executable file, hence the error. if you want to execute the java executable, then you need to actually provide the full path to it, probably "C:\Program Files\Java\jre7\bin\java.exe."
I'm developing a java desktop application to access information, format, change labels and volume id. This app's target are Windows users that will be using mainly Windows XP or Windows 7.
I'm getting basic info using java's File and FileSystemView; and I'm using Runtime.getRuntime().exec() to execute external Windows applications to do the other tasks.
I tried to use 'vol' and 'dir' to get the volumeId information but I get the following error:
Starting: vol E:
java.io.IOException: Cannot run program "vol": CreateProcess error=2, O sistema não pode encontrar o arquivo especificado
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 app.CommandRunnerWithReturn.run(CommandRunnerWithReturn.java:24)
Caused by: java.io.IOException: CreateProcess error=2, O sistema não pode encontrar o arquivo especificado
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
Is there a Java API or Windows program I can use to get volumeid info?
Thanks in advance.
Some commands are built in to a shell. To run these commands you need to run the shell to run the command. For CMD you need something like
CMD /C VOL
for unix shell you need something like
sh -c "cd /path ; command > file"