I am developing a Java application where one of its task is to execute a system command using Runtime exec. The command requires another JAR file to be able to work but whenever the part where the command will be executed, I am getting an "Error: Unable to access jarfile".
Firstly, my current working directory:
assets/jarFile.jar
myApp.jar
Here's what my code looks like:
try {
// To get the path of the directory where my current running JAR is
String jarPath = myApp.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String path = URLDecoder.decode(jarPath.substring(0, jarPath.lastIndexOf("/")), "UTF-8");
// The system command to execute
String command = "java -Xmx1024m -jar "+path+"/assets/jarFile.jar and-so-on...";
Process p = Runtime.getRuntime().exec(command);
...
}
The jarFile.jar is not an executable JAR by the way.
Any idea?
Seems like your code to find the path of the jar file does not return the correct path.
Related
I'm trying to use ProcessBuilder in order to create a tar.zst file that compresses a folder. However, I'm getting the following error after running this code:
try {
ProcessBuilder pb = new ProcessBuilder("tar","--zstd","-cf","info-x.tar.zst","tstpkg");
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
Process process = pb.start();
process.waitFor();
} catch (Exception e){
e.printStackTrace();
}
Error:
tar: Can't launch external program: zstd --no-check -3
And the tar file is created, but is empty.
If I run the same command from the terminal
tar --zstd -cf info-x.tar.zst tstpkg
then it works fine.
When you find a command that works "from the terminal" but not from Java then you should assume that the PATH or other environment required with that command is only set correctly via that terminal, and not when called by JVM.
So, one possible fix is to run your terminal indirectly which may fix the environment needed - depending on your shell and it's setup:
ProcessBuilder pb = new ProcessBuilder("bash", "-c", "tar --zstd -cf info-x.tar.zst tstpkg");
You might also try to fix by setting the correct PATH for running zstd as a sub-process of tar. Check where it is using which zstd and try adding to the PATH before pb.start():
String path = pb.environment().get("PATH"); // use Path on Windows
pb.environment().put("PATH", "/path/to/dir/ofzstd"+File.pathSeparator+path);
I have an .exe file on MyDocuments folder. I can't seem to run the program in the command prompt if I run C:\User\User\MyDocuments\Sample.exe. This gives me an error Failed to execute script.
But when I'll have the command prompt opened on the MyDocuments folder and only run Sample.exe, the programs runs perfectly.
What I want to do with this is I want to have a java program and execute Process p = Runtime.getRuntime().exec("C:\\User\\User\\MyDocuments\\Sample.exe"); and it will give me the Failed to execute script error.
Any I ideas what I missed?
As suggested by Application will not launch from command line full path, but will after CDing to directory I would make sure your executable does not depend on the current working directory.
Try Running:
Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
before your try and execute your executable.
After having used NetBeans to create a Java program call it Addition and then having successfully cleaned and built an Executable Jar File in a folder c:\Users\Ben\Doc\NetBeansProjects\Addition\dist
WHY is it that when executing, from command prompt,
c:\Users\Ben Java -Jar -cp "c:\Users\Ben\Doc\NetBeansProjects\Addition\dist" Addition.jar
it does NOT work (i get 'unable to access jarfile Addition.jar)
BUT if i use cd to change my current dir to c:\Users\Ben\Doc\NetBeansProjects\Addition\dist and THEN run 'java -jar Addition.jar' from there, the Addition program runs fine
The -classpath argument is ignored when you use the -jar option. See the documentation.
because java doesn't look in classpath to launch jar file for this command it needs file as input
so if you set the directory where your jar file is placed and try to execute java -jar command and expect it to pick up jar from that directory because it is in classpath it is not valid
you can give full path to jar like from any directory
java -jar c:\Users\Ben\Doc\NetBeansProjects\Addition\dist\Addition.jar
i have a jar file called bg.jar which prints some numbers. i want to call this bg.jar from a java program. i used this code
public class bg1 {
public static void main(String[] args) {
try{
// Process ps = Runtime.getRuntime().exec("java -jar bg.jar");
ProcessBuilder builder = new ProcessBuilder("java -jar bg.jar");
Process process = builder.start();
}catch(Exception e){
System.out.println("e: "+ e.toString());
}
}
}
i added bg.jar in the libraries folder of bg1 project. but when i run bg1.class it gives me this error:
e: java.io.IOException: Cannot run program "java -jar bg.jar": CreateProcess error=2, The system cannot find the file specified
how can i call bg.jar from bg1.class
I think the file not found error is that the system cannot find the "java" executable file. One way is to add the "java" directory path to the environment variable PATH. To find the variable, right click on the "My Computer"/Advanced system settings/Environment Variables/System variables. Then open a new console, run your Java program above.
Alternative way is probably to load the jar file to your Java program. And then run the static main method in your Java codes. To dynamically load java files, see How to load a jar file at runtime. The class that contains the main method can be found by parsing the META-INF/MANIFEST.MF file in the jar file.
ProcessBuilder builder = new ProcessBuilder("java -jar bg.jar");
Space does not work in process builder command, I tried using comma separated words, and it worked.
Something like this for command "make macosx"-
ProcessBuilder builder = new ProcessBuilder("make" , "macosx");
From a long time i am struggling for with this program. I am having a shell script which accepts parameters as version number and path for the files. then that script creates Zip file with the name of version number congaing all file files to the folder.
I have installed Cygwin on following path D:/cygwin. I am coping required files to same location where cygwin is installed D:\cygwin\bin
Command
D:/cygwin/bin/bash -c '/bin/test/app.sh 04.10 D:\cygwin\bin\ Test_files
Or Can any one please suggest how to run shell script in java using Cygwin.
Rewriting the Problem:-
When i am trying to run following Command in command prompt it gives error
sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
Error:-C:\Documents and Settings\sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
/usr/bin/app.sh: line 51: lib/lib.sh: No such file or directory
But if i run the same command at
D:cygwin\bin\Test>sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
It works fine. Can any one suggest me how to avoid this kind of errors.
Runtime run = Runtime.getRuntime();
Process p = run.exec("D:/cygwin/bin/bash -c \'/bin/test/app.sh 04.10 D:\cygwin\bin\ Test_files");
p.waitFor();