I am new to linux. I am trying to load a SO file in Ubuntu using Java. The file that I have specified in the java method "System.load(/home/ab/Downloads/libtesseract.so)" loads fine but its dependent so file placed in the same place as "libtesseract.so" is not found. Here is the error message I get. Error: UnSatisfiedLinkError and says "liblept.so.4" cound not be found. This so file is placed in the same location as libtesseract.so. When I place "liblept.so.4" in the "/lib". It is able to load this so file from. So what I understood is that for, its not for java to load the dependent so. It has to be loaded by ubuntu. So I tried a simple application to load this by setting the PATH variable with the location of the so file. And exported the java code into a jar and tried to run this jar file from terminal as the path variable is not persistent for entire system. It worked fine. So I tried to do the same thing programmatically by using the code below to its not working. Please advice. TIA
Code:
ProcessBuilder pb = new ProcessBuilder("/bin/sh");
Map<String, String> envMap = pb.environment();
envMap.put("LD_LIBRARY_PATH", "/home/ab/Downloads");
envMap.put("PATH", "/home/ab/Downloads");
Set<String> keys = envMap.keySet();
for(String key:keys)
{
System.out.println(key+" ==> "+envMap.get(key));
}
System.load("/home/ab/Downloads/libtesseract.so");
As far as I know you can't really modify the environment variables in Java "on-the-fly". That means you should set both LD_LIBRARY_PATH and PATH before running the java.
Related
I'm trying to execute java to run a JAR file from an Azure function, but it seems like the java PATH variable isn't defined or something because Azure can't seem to find it. Code below:
Process proc = new Process();
try
{
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.FileName = "java.exe";
proc.StartInfo.Arguments = "-jar file path and some more arguments";
proc.Start();
proc.WaitForExit();
if (proc.HasExited)
log.Info(proc.StandardOutput.ReadToEnd());
log.Info("Java Success!");
}
catch (Exception e)
{
log.Info("Java Fail");
log.Info(e.Message);
}
Even if I remove the proc.StartInfo.Arguments or tell it to use java.exe instead of java I still get the same error, below:
[Info] The system cannot find the file specified
Is calling java not supported in Azure functions?
OK figured this out. So far, the best way is to fully qualify the path to java.exe...
so I changed
proc.StartInfo.FileName = java.exe";
to
proc.StartInfo.FileName = "D:\\Program Files (x86)\\Java\\jdk1.8.0_73\\bin\\java.exe";
You can figure out the full path to Java using KUDU, which is https://[yourFunctionName].scm.azurewebsites.net/
If you click on Tools->DebugConsole, you can browse until you find where Java is located.
Note hard coding the path to Java is probably a bad idea so you should probably use application settings.
Edit Below is a link to a GitHub Repo with my final project. The project does some other stuff, but you can see where I call Java to execute the Batik JAR.
https://github.com/osuhomebase/SVG2PNG-AzureFunction
As of September 2022,
proc.StartInfo.FileName = java.exe";
Worked fine for me. It appears the Azure Function Environment has the JDK installed and the JAVA PATH variable set.
Im using Coldfusion 9 with Multiserver/J2EE on JRun 4 Configuration in a development engine.
I try to apply sample below into my coldfusion engine however it not work.
Sample : How do you use java files in Coldfusion
Perhaps anyone here can help me to resolve this issue?
Here is what i did :
1) Write a simple Hello.java file, compile into Hello.class file.
public class Hello
{
public String testJava()
{
return "Hello Java!!";
}
}
2) Write a cfm file : jHello.cfm to call the java object.
<cfscript>
helloWorld = CreateObject("java","Hello");
helloTest = helloWorld.testJava();
</cfscript>
3) Save the .class file into class path :
4) Restart coldfusion 9 Server
5) However, it return Error below when run JHello.cfm file :
Object Instantiation Exception.
Class not found: Hello
The error occurred in C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs/accuity_dev/JHello.cfm: line 2
1 : <cfscript>
2 : helloWorld = CreateObject("java","Hello");
3 : helloTest = helloWorld.testJava();
4 : </cfscript>
Appreciate your time, hope can get back from any expert here.
Do you have access to CF administrator page? I see a ear/war in path indicating a different configuration than what I have tried or used before. I have done multiple instances but not with J2EE ear/war.
If you can access administrator page, then go to java/jvm settings page. In that, put the full absolute path of the .class file and hit submit. You will have to restart CF.
Note : this is no different than editing the jvm.config file but you dont have to worry about syntax especially windows vs linux, forward slash or back slash.
Finally, please make a backup of jvm.config before making any changes via CF administrator.
I am trying to execute hadoop fs -put <source> <destination> from Java code. When I execute this command directly from the terminal, it works fine but when I try to execute this command from within the Java code using
String[] str = {"/usr/bin/hadoop","fs -put", source, dest};
Runtime.getRuntime().exec(str);
I get error as Error: Could not find or load main class fs. I tried to execute some non-hadoop commands like ls,mkdir commands from Java and they worked fine but the hadoop commands are not getting executed even though they work fine from the terminal.
What could be the possible reason for this and how can I solve it?
JAVA API TRY: I tried to use java api to perform the copy operation but I get error. The Java code is :
String source = "/home/tmpe/file1.csv";
String dest = "/user/tmpe/file1.csv";
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://node1:8020");
FileSystem fs = FileSystem.get(conf);
Path targetPath = new Path(dest);
Path sourcePath = new Path(source);
fs.copyFromLocalFile(false,true,sourcePath,targetPath);
The error which I get is:
Exception in thread "main" java.io.IOException: Mkdirs failed to create /user/tmpe
at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:378)
at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:364)
at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:564)
at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:545)
at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:452)
at org.apache.hadoop.fs.FileUtil.copy(FileUtil.java:229)
at org.apache.hadoop.fs.FileSystem.copyFromLocalFile(FileSystem.java:1230)
I have already created /user/tmpe folder and it has full read-write permissions but still this error comes. I am unable to get the issue resolved
I guess you probably do not have a HADOOP_HOME environment variable set.
But since you're in Java, why on earth would you want to do a haddop fs -put in an external process when the Java API is even more friendly than the shell ?
Came across old post but if you haven't tried already, execute it with hadoop jar app_name.jar instead of java -jar. This way if classpath of your jar does not have all hadoop jars it will fetch the jars predefined in $HADOOP_CLASSPATH.
On a particular server (Windows 2012 server R2) I am having trouble creating a temp file. I get the following error everytime I try.
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createTempFile(Unknown Source)
etc..
The error happens everytime the following code is ran:
InputStream inputStream = portalBean.createPDF( sessionID, foCode );
Things I have tried
Changed the java.io.tmpdir variable on the fly. System.setProperty("java.io.tmpdir", "C:\\");
Added -Djava.io.tmpdir=c:\\temp to the webnetwork lax file to an unrestricted location.
I tried setting the webNetwork service to run as a specified user with rights to temp files e.g. the Administrator.
Made sure I have free disk space and I cleaned out the c:\windows\temp folder.
Made sure the tmp environment variables were set to their default values.
I also tried running the service from a command prompt which was opened with the Run As Administrator option.
And the IOException lingers still. I have another server running the same code without issue (Windows Server 2012).
Does anyone else have any Ideas of what else I can try to resolve this issue? And or any tips on how I can debug the issue more thoroughly to get a grasp of what is going on?
One tool you can use to debug this is process monitor from system internal tool kit. The step is: add a filter to only monitor your process (I think it is javaw.exe in your case), after the error happens, go through the file activities in the process monitor log, you can find how the process is finding files and which directories the process searched. If the process is searching in the wrong directory, you can find it from the log.
I just used this tool to figure out a JVM crash problem today.
Based on the description of your problem, I guess the path variable of the process is changed in the middle of your code, with another tool process explore you can view the path variable of the process, it might help.
Try and create instead a directory somewhere under your home directory:
final Path tmpdir = Paths.get(System.getProperty("user.home"), "tmp");
Files.createDirectories(tmpdir);
System.setProperty("java.io.tmpdir", tmpdir.toAbsolutePath().toString());
Then try and Files.createTempFile() in there.
Note that if Files.createDirectories() refers to an existing file which is not a directory, you'll get a FileAlreadyExistsException.
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.