How to run openssl command from JAVA using Runtime? - java

I want to read private and public key generated using PuttyGen for that I used openssl to convert them into DER format.
String[] execStr = {"openssl","pkcs8", "-topk8", "-inform", "PEM", "-outform","DER", "-in", "src\\srcData\\openssh1\\privateKey.pem","-out", "src\\srcData\\openssh1\\privateKey.der" };
File execDir = new File("C:\\openssl-1.0.2d-fips-2.0.10\\bin");
Runtime.getRuntime().exec(execStr,null,execDir);
But I am getting this error:
Exception in thread "main" java.io.IOException: Cannot run program "openssl" (in directory "C:\openssl-1.0.2d-fips-2.0.10\bin"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1059)
at java.lang.Runtime.exec(Runtime.java:631)
at PrivateKeyReader.get(PrivateKeyReader.java:21)
at Test1.main(Test1.java:50)
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:455)
at java.lang.ProcessImpl.start(ProcessImpl.java:151)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1040)
... 3 more
Here I am not able figure out the exact issue, please let me know if anyone know it.

I understand you downloaded the Windows version of openssl-1.0.2d-fips-2.0.10. The executable contained in the bin folder is called openssl.exe, not openssl. For this reason, you get the error The system cannot find the file specified. Your execStr should therefore be String[] execStr = {"openssl.exe", ...
To prevent this issue in the future, you can make Windows Explorer show the full openssl.exe name instead of openssl, using the instructions here.
Also note that when you use C:\\openssl-1.0.2d-fips-2.0.10\\bin as the execDir, the path src\\srcData\\openssh1\\privateKey.pem is interpreted relative to execDir. You should therefore turn it into an absolute path using:
File inputFile = new File("src\\srcData\\openssh1\\privateKey.pem");
String[] execStr = {"openssl.exe ", ..., "-in", inputFile.getCanonicalPath(), ... };
The same also applies to the output file.

Related

Getting No such File or Directory Exception on Ubuntu when trying compile a java project from java.lang.Process

Recently, I have switched to Ubuntu v20.04 from Windows 10 Pro v2004 because of performance purposes. When, I was on Windows I can freely compile a java project from another java program by writing:
String pathToCompiler = "\"C:/Program Files/Java/jdk-14/bin/javac\"";
Process compileProcess = Runtime.getRuntime().exec(pathToCompiler+" -d bin #.sources", null, new File("ProjectPath"))
Where the sources file is a file containing the list of classes of the project
The code above works successfully on Windows 10.
But On Linux(Ubuntu):
if I substitute the value of variable pathToCompiler as
pathToCompiler = "\"/usr/lib/jvm/java-11-openjdk-amd64/bin/javac\""
the below exception is raised up and the program executing the command exits:
"/usr/lib/jvm/java-11-openjdk-amd64/bin/javac" -d bin #.sources
java.io.IOException: Cannot run program ""/usr/lib/jvm/java-11-openjdk-amd64/bin/javac"" (in directory "/home/arham/Documents/Omega Projects/Project0"): error=2, No such file or directory
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
at java.base/java.lang.Runtime.exec(Runtime.java:592)
at java.base/java.lang.Runtime.exec(Runtime.java:416)
at ide.utils.systems.BuildView.lambda$3(BuildView.java:267)
at java.base/java.lang.Thread.run(Thread.java:834)
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:340)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:271)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
The problem is that the file actually exists but it says No Such File or Directory
Actually, The program which is compiling the project is a Java IDE that I am creatiing.
Someone please tell if he/she knows how to fix this bug
The Runtime.exec method has several problems that make it difficult to use, and this is one of them. Use the newer ProcessBuilder class instead.
String pathToCompiler = "C:/Program Files/Java/jdk-14/bin/javac";
Process compileProcess = new ProcessBuilder(pathToCompiler, "-d", "bin", "#.sources")
.directory(new File("ProjectPath"))
.start();
The differences are:
Remove the extra quotes from around the path to the executable. If quoting is needed, the system takes care of it.
Pass the each command line arguments as a separate string. This way you don't have to worry about quoting.
Update the path to the following:
String pathToCompiler = "/usr/lib/jvm/java-11-openjdk-amd64/bin/javac/";

The system cannot find the file specified at java.lang.ProcessBuilder.start

I am a novice at programming. I have recently tried to download the source code of an open source software and setup the environment. However, I am seeing the following issue when I tried to run the build for the first time:
Execute failed: java.io.IOException: Cannot run program "unzip" (in directory "....\"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start
Any tips on how to resolve this issue?
From what I understand, it is unable to a file:
where exactly to look for ProcessBuilder.start
How do I modify it?
Try this:
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("Write command here").start();
Edit: Command - string array which begin with fullpath to your programm. For example:
processBuilder.command("C:\\Program Files (x86)\\Microsoft Office\\Office15\\OUTLOOK.exe").start();

Running yarn job from java program using ProcessBuilder gives file does not exist error

I am trying to run a yarn job from a java wrapper program. The mapreduce jar takes two inputs:
A header file: I dont know the name of the file but the location and file extension and there's only one file at that location
A Input files directory
Apart from these I have an Output directory.
the processbuilder code looks like:
HEADER_PATH = INPUT_DIRECTORY+"/HEADER/*.tsv";
INPUT_FILES = INPUT_DIRECTORY+"/DATA/";
OUTPUT_DIRECTORY = OUTPUT_DIRECTORY+"/";
ProcessBuilder mapRProcessBuilder = new ProcessBuilder("yarn","jar",JAR_LOCATION,"-Dmapred.job.queue.name=name","-Dmapred.reduce.tasks=500",HEADER_PATH,INPUT_DIRECTORY,OUTPUT_DIRECTORY);
System.out.println(mapRProcessBuilder.command().toString());
Process mapRProcess = mapRProcessBuilder.start();
On run, I get the following error:
Exception in thread "main" java.io.FileNotFoundException: Requested
file /input/path/dir1/HEADER/*.tsv does not exist.
But when I run the same command as :
yarn jar jarfile.jar -Dmapred.job.queue.name=name -Dmapred.reduce.tasks=500 /input/path/dir1/HEADER/*.tsv /input/Dir /output/Dir/
It works all fine.
what can be the issue when running the command from java is causing this issue?
The * is being treated as part of the literal string in this case rather than a wildcard. Therefore globbing isn't expanding to your desired path name.
If there is only one file in the directory, why don't you find what its path is and pass that as the argument instead
eg.
File dir = new File(INPUT_DIRECTORY+"/HEADER);
if (dir.list().length > 0)
String HEADER_PATH = dir.list()[0].getAbsolutePath();

Running external executable file from Java program

I'm trying to write a Java program which gets a executable file to run under Linux. This executable file receives two parameters and performs a nmap -sP operating with the two given parameters.
I've called this file as file.exe and its content is the one below:
nmap -sP $segment1-$segment1
I already did a chmod +x file.exe and it's in same directory where the .class is present.
The Java code is the following:
import java.lang.Runtime;
import java.lang.Process;
import java.io.IOException;
import java.lang.InterruptedException;
public class runFile {
public static void main (String args[]) throws IOException, InterruptedException {
Runtime r = Runtime.getRuntime();
Process p = r.exec("file.exe "+args[0]+" "+args[1]);
p.waitFor();
}
}
After compiling, Whenever I try to run it (from where the file.exe is) by
java runFile
I'm getting the following exception and errors log:
Exception in thread "main" java.io.IOException: Cannot run program
"file.exe": error=2, No such file or directory at
java.lang.ProcessBuilder.start(ProcessBuilder.java:1041) at
java.lang.Runtime.exec(Runtime.java:617) at
java.lang.Runtime.exec(Runtime.java:450) at
java.lang.Runtime.exec(Runtime.java:347) at
runFile.main(runFile.java:12) Caused by: java.io.IOException: error=2,
No such file or directory at java.lang.UNIXProcess.forkAndExec(Native
Method) at java.lang.UNIXProcess.(UNIXProcess.java:135) at
java.lang.ProcessImpl.start(ProcessImpl.java:130) at
java.lang.ProcessBuilder.start(ProcessBuilder.java:1022) ... 4 more
What am I missing?
The error is telling you that the executable can not be found in the current directory or the OS's search path.
Try including the Pathans part of the command
Process p = r.exec("/path/to/file.exe "+args[0]+" "+args[1]);
You should also consider separating each command/argument as a separate parameter
Process p = r.exec(new String[]{"/path/to/file.exe ", args[0], args[1]});
This will help with parameters that contain spaces.
You should also consider using ProcessBuilder, this will allow you to change the directory context that the command should be executed
Your java program looks for file.exe in the directory where you started your java program. It does not look inside the directory with your class files.

using matlabcontrol API to call to call matlab function from Java within Netbeans

I have been trying to edit the the following matlabcontrol code but still there is an error when I run it. Please friends help me out!
package matcontro;
import matlabcontrol.*;
public class HelloWorld
{
public static void main(String[] args) throws MatlabConnectionException, MatlabInvocationException
{
// create proxy
MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder()
.setUsePreviouslyControlledSession(true)
.build();
MatlabProxyFactory factory = new MatlabProxyFactory(options);
MatlabProxy proxy = factory.getProxy();
// call builtin function
proxy.eval("disp('hello world')");
// call user-defined function (must be on the path)
proxy.eval("addpath('C:\\ Users\\HASENDE\\My Documents\\MATLAB')");
proxy.feval("myfunc");
proxy.eval("rmpath('C:\\ Users\\HASENDE\\My Documents\\MATLAB')");
// close connection
proxy.disconnect();
}
}
The error that I get is below;
run:
Exception in thread "main" matlabcontrol.MatlabConnectionException:
Could not launch MATLAB. Command: [matlab, -r, javaaddpath
'C:\Users\HASENDE\Documents\NetBeansProjects\Java Classpath
Libraries\matlabcontrol-4.0.0.jar';
matlabcontrol.MatlabClassLoaderHelper.configureClassLoading();
javarmpath 'C:\Users\HASENDE\Documents\NetBeansProjects\Java Classpath
Libraries\matlabcontrol-4.0.0.jar';
matlabcontrol.MatlabConnector.connectFromMatlab('PROXY_RECEIVER_01caa56d-9ed7-4e39-a45b-345051024d49',
2100);]
at
matlabcontrol.RemoteMatlabProxyFactory.createProcess(RemoteMatlabProxyFactory.java:305)
at
matlabcontrol.RemoteMatlabProxyFactory.requestProxy(RemoteMatlabProxyFactory.java:116)
at
matlabcontrol.RemoteMatlabProxyFactory.getProxy(RemoteMatlabProxyFactory.java:134)
at matlabcontrol.MatlabProxyFactory.getProxy(MatlabProxyFactory.java:81)
at matcontro.HelloWorld.main(HelloWorld.java:21)
Caused by: java.io.IOException: Cannot run program "matlab": CreateProcess error=2, The system cannot find the file specified at
java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) at
matlabcontrol.RemoteMatlabProxyFactory.createProcess(RemoteMatlabProxyFactory.java:292)
... 4 more
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
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
The issue is that matlabcontrol on Windows and Linux expects 'matlab' to be understood due to the MATLAB directory being part of your PATH environment variable. This exception is indicating that is not the case. That's fine, you just need to explicitly set the location of where your MATLAB executable is. From the javadoc for setMatlabLocation(...):
Sets the location of the MATLAB executable or script that will launch MATLAB. If the value set cannot be successfully used to launch MATLAB, an exception will be thrown when attempting to create a proxy.
The absolute path to the MATLAB executable can be determined by running MATLAB. On OS X or Linux, evaluate [matlabroot '/bin/matlab'] in the Command Window. On Windows, evaluate [matlabroot '/bin/matlab.exe'] in the Command Window. The location provided does not have to be an absolute path so long as the operating system can resolve the path.
Windows
Locations relative to the following will be understood:
The current working directory
The Windows directory only (no subdirectories are searched)
The Windows\System32 directory
Directories listed in the PATH environment variable
App Paths defined in the registry with key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
By default on Windows, MATLAB places an App Path entry in the registry so that matlab can be used to launch MATLAB. If this property is not set, this App Path entry will be used.
OS X
Locations relative to the following will be understood:
The current working directory
Directories listed in the PATH environment variable
On OS X, MATLAB is installed in /Applications/ as an application bundle. If this property is not set, the executable inside of the application bundle will be used.
Linux
Locations relative to the following will be understood:
The current working directory
Directories listed in the PATH environment variable
During the installation process on Linux, MATLAB can create a symbolic link named matlab that can be used to launch MATLAB. If this property is not set, this symbolic link will be used.
Just to complement the answer, I had a similar problem (I am using Intellij IDEA and Matlab R2014a). Indeed the exact path of the program was missing from the Enviromental Variable Path.Some matlab paths can be found (or automatically written when installing matlab) like "C:\Program Files\MATLAB\MATLAB Runtime\" or "C:\Program Files\MATLAB\MATLAB Compiler\" but only the one that holds the .exe work, like "C:\Program Files\MATLAB\R2014a\bin". Yet, my program didn't work till I re-start the IDE. Keep that in mind.

Categories

Resources