the java code is
String SCRIPT_PATH = "D:\\codes\\Python\\StockDataAnalysis\\MatGraph.py";
try {
Jep jep = new Jep();
jep.runScript(SCRIPT_PATH);
} catch (Exception ex) {
ex.printStackTrace();
}
and it throws exception:
ImportError: No module named site
Python is no doubt installed correctly, for I can run python project on PyCharm. Jep is also set correctly, with lib path added to the PATH in windows System
You could try creating instance of Jep like so:
new Jep(false, path);
where as per javadoc (https://github.com/mrj0/jep/blob/master/src/jep/Jep.java#L299):
#param interactive
whether {#link #eval(String)} should support the slower
behavior of potentially waiting for multiple statements #param includePath
a path of directories separated by File.pathSeparator that
will be appended to the sub-intepreter's sys.path
This approach worked for me in similar case. In my case path was pointing to a directory
...virtualenv\projectname\Lib\site-packages
created for my virtual env where all my python libs for my project are stored.
Please notice that you need to install jep inside of that virtual env first in order to make it work.
Related
I'm trying to connect to my embedded H2 database via Java. I found various threads and tutorials on this and now have this code:
Connection con = null;
Properties connectionProps = new Properties();
connectionProps.put("user", "username");
connectionProps.put("password", "password");
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
con = DriverManager.getConnection("jdbc:h2:~/test", connectionProps);
I got the "no suitable driver found for jdbc:h2:~/test" error message. I found the Class.forName(...) in some threads as a solution to this, but it doesn't seem to be working (ClassNotFoundException).
I read that the driver is probably not in my classpath, but don't really know what I need to do with that information. In the database view of IntelliJ the driver seems to work just fine (I can click reload drivers and it confirms the h2 driver). What am I doing wrong?
There are a couple ways to add to the classpath. I recommend trying to understand this page https://howtodoinjava.com/java/basics/java-classpath/ for more information on different strategies that have similar, but different results.
Probably the simplest way for just this project, would be to edit the "run configuration" that you are using, and add a new VM option:
–classpath /path/to/local/h2/driver.jar
Use an absolute path to the jar, and this will allow it to be present in the classpath when running it this way. In the link I shared, this is the same as adding the command line argument to the java or javac command, so this argument will work outside of intellij on the command line as well.
Edit:
Based on the comments, it seems like you might be using the %PROGRAMFILES% environment variable, with a fully qualified path.
Try this instead:
-classpath %PROGRAMFILES%\H2\bin\h2-1.4.200.jar
If you were to run the following command in your command prompt
echo %PROGRAMFILES%
I suspect you'd get the response:
C:\Program Files
Which is not the x86 version. So. Either use the environment variable and omit the part of the path it's value represents (eliminating the C:\ part)
or, if that's not the correct Program Files folder at all, then avoid it and try (note the quotes which are required because a folder has spaces in it's name):
--classpath "C:\Program Files (x86)\H2\bin\h2-1.4.200.jar"
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.
I'm trying to install TensorFlow for Java on Windows 10 using this Article
. I followed the steps carefully but the windows commands didn't work with me so I decided to do it manually.
The first command is to make the .jar part of the classpath and I did it manually
but the second step was to ensure that the following two files are available to the JVM: the .jar file and the extracted JNI library
but I don't know how to do that manually
The code:
package securityapplication;
import org.tensorflow.TensorFlow;
import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
public class SecurityApplication {
public static void main(String[] args) throws Exception {
try (Graph g = new Graph()) {
final String value = "Hello from " + TensorFlow.version();
// Construct the computation graph with a single operation, a constant
// named "MyConst" with a value "value".
try (Tensor t = Tensor.create(value.getBytes("UTF-8"))) {
// The Java API doesn't yet include convenience functions for adding operations.
g.opBuilder("Const", "MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build();
}
// Execute the "MyConst" operation in a Session.
try (Session s = new Session(g);
Tensor output = s.runner().fetch("MyConst").run().get(0)) {
System.out.println(new String(output.bytesValue(), "UTF-8"));
}
}
}
}
could someone help? cuz my program that uses TensorFlow still have the following error
The text in the image is :
Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: windows, architecture: x86. See https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java/README.md for possible solutions (such as building the library from source). Additional information on attempts to find the native library can be obtained by adding org.tensorflow.NativeLibrary.DEBUG=1 to the system properties of the JVM.
at org.tensorflow.NativeLibrary.load(NativeLibrary.java:66)
at org.tensorflow.NativeLibrary.load(NativeLibrary.java:66)
at org.tensorflow.TensorFlow.init(TensorFlow.java:36)
at org.tensorflow.TensorFlow.<clinit>(TensorFlow.java:40)
at org.tensorflow.Graph.<clinit>(Graph.java:194)
at securityapplication.SecurityApplication.main(SecurityApplication.java:15) Java Result: 1 BUILD SUCCESSFUL (total time: 4 seconds)
The result after running the first command in cmd:
The result after running the second command in Windows PowerShell:
Any suggestions?!
Thank you
The first command failure (javac) suggests that the javac command is not in your PATH environment variables. See for example, this StackOverflow question
For the second command failure, I believe the space after -D is what is causing you trouble as Holger suggested.
IDEs like Eclipse and others also provide a means to set the java.library.path property for the JVM (see this StackOverflow answer for example).
Background: TensorFlow for Java consists of a Java library (packaged in a .jar file) and a native library (.dll on Windows, distributed in a .zip file). You need to ensure that the .jar file is in the classpath and the directory containing the .dll is in included in the java.library.path of the JVM when executing a program.
Hope that helps.
I made a little program and it worked fine, but now. First, it mux the xml chapter file in the mkv file, so we get a muxed mkv file. Some day ago I updated java to 1.7.21 and I think this is the problem why it is not working now. It's a little strange, but when I run in netbeans everything is fine, but when I build and I run the .jar file, it is not working. It create the xml file, but not mux in the mkv file (and because not muxed not delete the xml file). Here is the code: (filename=xml file path; mkv=mkv file path)
public void muxing() {
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("c:\\Program Files\\MKVtoolnix\\mkvpropedit.exe --chapters \""+filename+"\" \""+mkv+"\"");
if (p.waitFor()==0) {
File xmlfile=new File(filename);
xmlfile.delete();
}
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
The program worked with java 1.6 and I think with 1.7.17 too. Win7 32bit. Sorry for my bad English.
Oracle has made breaking changes to Runtime.exec() in Java 7 update 21 (and 6 update 45).
If the program name contains spaces, you need to specify command and arguments in an array:
Process p = Runtime.getRuntime().exec(new String[] {
"C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\""});
Another option is to use java.lang.ProcessBuilder:
Process p = new ProcessBuilder("C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\"").start();
As stated by Oracle:
Applications that need to launch programs with spaces in the program name should consider using the variants of Runtime.exec that allow the command and arguments to be specified in an array.
Alternatively, the preferred way to create operating systems processes since JDK 5.0 is using java.lang.ProcessBuilder. The ProcessBuilder class has a much more complete API for setting the environment, working directory and redirecting streams for the process.
I have a set of unit test cases that depend on a test.properties file. When I run the tests under Mac OSX or Linux using Maven ('mvn test'), they work fine. But when running under Windows 7, they can't find the file unless I copy it directly to the class folder. The code to return the properties is the following two methods:
private void loadProperties() {
try {
properties.load(HibernateTestCase.class.getResourceAsStream(getPropertiesFilePath()));
} catch (Exception ioExc) {
ioExc.printStackTrace();
}
}
private String getPropertiesFilePath() {
return File.separator + "test.properties";
}
What's the real deal here? Is it all about the file path being set wrong somewhere? Thanks in advance!
The separator in resource names is always '/'. File.separator varies from platform to platform (on UNIX variants it will generally be /, on Windows it will not).
Either your classpath is different, or you're using a different classloader with different resolution characteristics.