I'm using the following command in runtime to extract a jar to a specific path, but the file is extracted in my class exe path.
cmd="cmd /c cd F: && cd F:\workFolder\ProcessFile\ProcessJar\PJar && jar xvf F:\workFolder\ProcessFile\ProcessJar\agconfig.jar"
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
Suppose, I'm executing the above code in E:\, then the jar file is extracted in the same path.
Kindly help me to extract the jar in specified path (F:\workFolder\ProcessFile\ProcessJar\PJar)
You should not make system calls from a java application, if you can also solve the problem using java code.
You can do it in java with java.util.zip.*.
See this tutorial.
Related
My requirement is i have to run Batch file located in (C:\Users\Vk\TestBatch.bat) under C:\users\VK\Logs(This is the path in which i have to run the batch file). After that i have to run the python script. I have to do this by using ProcessBuilder. Below is the piece of code i am using, but unfortunately it is not working.
String[] command ={"cmd.exe","/C","cd C:\\Users\\vk\\Logs","C:\\users\\Vk\\TestBatch.bat",
"C:\\Python27\\ArcGIS10.3\\python.exe","C:\\Users\\vk\TestScript2.py"};
probuilder = new ProcessBuilder(command );
Can any one suggest me how to run this batch file in other location and followed by the execution of python script.
Thanks,
Sudheer
There are two options to achieve this.
Either add your python executable in your System Path.
You can go to the particular directory where executable is located and run the python script.
a. Go to Directory where batch file is located.
b. Run batch file by simply putting the name.
c. go to home directory by command cd
d. go to directory where python executable is present.
e. run python script using command `py <scriptName>`
String [] command = {"cd C:\users\Vk", "TestBatch", "cd", "cd C:\Python27\ArcGIS10.3\", "py TestScript2.py"};
ProcessBuilder probuilder = new ProcessBuilder(command);
I am facing a problem with a .bat file run from Java web application.
Current setup in local machine: create web application and method contains
Process p = Runtime.getRuntime().exec("c:/test/myFile.bat");
When I run above code, its showing file not found.
But if I copied the .bat file to tomcat bin folder it's working fine. But I need to execute the bat file in my separate folder, I even set the class path also, but I'm not able to execute the bat.
Screen 1:
I have placed 5 files in Tomcat root directory din folder, as well as placed at c:/psgsscripts/--> folder also (check the Java code; only bat file is taken other .tbc files are looking at Tomcat bin folder location at run time)
Screen 2:
bat file contains tclsh psg.tbc %1
Screen 3:
web application java code in class method( newjobid is the parameter for .bat file)
Screen 4:
If I placed it all .bat file and .tbc files bin folder it's working fine (Java code represent read the .bat file from c drive but other .tbc files look at Tomcat bin folder at run time)
but requirement is those files are placed at other drives (other than Tomcat folder) like C or D or E drivers.
You need to execute the command to run the batch file like below
Runtime.getRuntime().exec("cmd /c start c:\\test\\myFile.bat");
Try the following:
Process p = Runtime.getRuntime().exec("cmd /C start c:/test/myFile.bat");
If you look at the java docs, the method exec expects an OS command and not the file name. So in the proposed solution, "cmd" is the OS command, /C is a switch that tells the OS command to carry out the command specified by string and then terminate. Here the command specified by the string is start. The command "start" requires a file name with full path as its parameter.
I am trying to clear the archive bit in some files in windows 7 64bit. I have tried the code:
atrrib -a * /s
The error I get: 'atrrib' is not recognized as an internal or external command,
operable program or batch file.
I am having a small java code going through the file. If there is a way to do it in java would be preferable.
In Java7 you can do this:
File theFile = new File("c:/foobar.txt");
Path file = theFile.toPath();
Files.setAttribute(file, "dos:archive", false);
Too many r's, try this:
attrib -a * /s
Run with the full path.
C:>%SystemRoot%\system32\attrib
There is an issue with the environment variable path and the use of attrib.
See this link for more detail.
http://social.technet.microsoft.com/Forums/windows/en-US/63b84992-3814-4c00-acbf-fc09816570e5/problem-in-path-settings-windows7?forum=w7itprogeneral
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";
I am using the Java code to execute a bat file to make a war file by the following java code
String command = "cmd /C start C:/processFolder/paas.bat";
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
which means it will execute the paas.bat file to do some jobs. There is a command within the paas.bat :
jar cfv xxx.war */ .
however, it does not jar all the file and folder into xxx.war. Does any one has the idea why this happens? Thanks!
The command uses current working directory, which will be different from java program. You may change script to do a cd and then call jar command.
cd /d <path to folder where this should execute from>
jar ....
Please capture output , error and exceptions(if any) from the command and add to your question.
(note#1. It may be more useful to use tools like ant to do these task. The tasks in ant can be called from java program using ant libraries..
Note#2 - Prefer ProcessBuilder to launch new process. )