Execute multiple functions at same time in Jython 2.5 - java

I'm trying to run multiple Jython files at the same time so that I can use my PC multiprocessor (specifically doing this in FDM in Hyperion's Workspace)
Is there any way I could do this?
I've tried to do it through Java, but it doesn't recognize the thread function, also tried through Python, and this version of Jython doesn't have the concurrency library, and not able to import it.
import os
import sys
from java.io import *
from java.util import *
from java import *
from java.lang import *
from threading import *
import java.util.logging.Level;
import java.util.logging.Logger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
new Thread() {
public void run() {
java.lang.Runtime.getRuntime().exec("python test1.py")
}
}.start()
new Thread() {
public void run() {
java.lang.Runtime.getRuntime().exec("python test2.py")
}
}.start()
new Thread() {
public void run() {
java.lang.Runtime.getRuntime().exec("python test3.py")
}
}.start()
Errors:
File "E:\Oracle\Middleware\EPMSystem11R1\products\FinancialDataQuality\Applications\FDMEE/data/scripts/custom/test.py", line 15
new Thread() {
^
SyntaxError: mismatched input 'Thread' expecting NEWLINE

You cannot use Java syntax in python code. Even if you're running it with Jython.
You can use the fact that Jython will convert python functions to Java functional interface.
from java.lang import Thread, Runtime
Thread(lambda: Runtime.getRuntime().exec("python test1.py")).start()
Thread(lambda: Runtime.getRuntime().exec("python test2.py")).start()
Thread(lambda: Runtime.getRuntime().exec("python test3.py")).start()
Pythonic way to do the same would be
import subprocess, threading
threading.Thread(target=lambda: subprocess.call(["python","test1.py"])).start()
threading.Thread(target=lambda: subprocess.call(["python","test2.py"])).start()
To be honest I would use multiprocessing instead of threading, but I am not sure if Jython supports it.

Related

How can I fix the project build path error? I can't import the java.lang.math for a Java 8/9 Calculator app

I am developing a simple calculator application in Java 8/9 in Eclipse. I am working on the power operation (as in "to the power of" used in math). I want to use the Math.power() instead of a for loop. However, I am having trouble importing the java math package into the program. The internet says to add import java.lang.math. When I try to code it in, I receive a notice of "Cannot Perform Operation. This compilation unit is not on the build path of the Java Project". What am I overlooking and/or doing wrong? Please provide suggestions or feedback.
Please note: Yes this is an academic assignment. To make this clear, I am not asking for the coding of the power operation. This issue is specifically about the importing the math package.
power operation (power.java)
package org.eclipse.example.calc.internal.operations;
import org.eclipse.example.calc.BinaryOperation;
// import java.lang.math; produces error
// Binary Power operation
public class Power extends AbstractOperation implements BinaryOperation {
// code removed. not relevant to SOF question.
}
Main (calculator.java)
package org.eclipse.example.calc.internal;
import org.eclipse.example.calc.BinaryOperation;
import org.eclipse.example.calc.Operation;
import org.eclipse.example.calc.Operations;
import org.eclipse.example.calc.UnaryOperation;
import org.eclipse.example.calc.internal.operations.Power;
import org.eclipse.example.calc.internal.operations.Equals;
import org.eclipse.example.calc.internal.operations.Minus;
import org.eclipse.example.calc.internal.operations.Plus;
import org.eclipse.example.calc.internal.operations.Divide;
import org.eclipse.example.calc.internal.operations.Square;
public class Calculator {
private TextProvider textProvider;
private String cmd;
private boolean clearText;
private float value;
public static String NAME = "Simple Calculator";
public Calculator(TextProvider textProvider) {
this.textProvider = textProvider;
setupDefaultOperations();
}
private void setupDefaultOperations() {
new Power();
new Equals();
new Minus();
new Plus();
new Divide();
new Square();
}
....
BTW, I use camel Case normally, but the academic project name everything including file names in standard writing format.
EDIT: After reading a response, I realized I forget to mention this. I can't get any further than typing import java., then the error pop-ups. Then I can't type the rest of the import statement
Image of package hierarchy
Your project is not configured correctly. You have no source dir at all. The src dir should be marked as source dir; right click it and tell eclipse about this, or, as it is a maven project, it's more likely a broken pom. Also, why are you using the org.eclipse package? If you work for SAP, it should be com.sap.

How to use imports in Jython, used as standalone jar?

I want to run a Java code, which is :
import org.python.util.PythonInterpreter;
public class PyConv {
public static void main(String[] args){
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("Test.py");
}
}
And the Test.py file;
import pandas
import numpy
df = pd.read_csv("Myfile.csv", header=0)
But I would get an error:
Exception in thread "main" Traceback (most recent call last):
File "Test.py", line 1, in <module>
import pandas
ImportError: No module named pandas
So, How would one import the required module, to make the code run?
And also I am using the Jython as a external jar in my java code. Is there any other way which would make my job simpler?

Can the package "org.opencv.core.Mat" be used for a simple Java program?

I'm trying to do some operations with a Matrix in Java using opencv. I'm using Eclipse Kepler IDE.
The problem happens when I try to declare a new matrix with the constructor, then I get the following error in the console:
Exception in thread "main" java.lang.UnsatisfiedLinkError:
org.opencv.core.Mat.n_Mat(III)J
at org.opencv.core.Mat.n_Mat(Native Method)
at org.opencv.core.Mat.<init>(Mat.java:477)
I'm using OpenCV 2.4.8 for OSX, OSX 10.9.1 and Eclipse Kepler.
Here is my Code:
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
public class FisherFaces {
public static void main(String[] args) {
Size s = new Size(new double[] {3,3});
Mat g= new Mat(3,3,CvType.CV_8UC1);
}
Is there anything I am doing wrong to cause this error?
I found the problem, I wasn't loading the native libraries, adding the line below fixes it.
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

DELETE_ON_CLOSE deletes files before close on Linux

I have this following code using Java 7 nio API:
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
public class TestDeleteOnClose {
public static void main(String[] args) throws IOException {
Path tmp = Files.createTempFile("a", "b");
OutputStream out = Files.newOutputStream(tmp, StandardOpenOption.DELETE_ON_CLOSE);
ObjectOutputStream os = new ObjectOutputStream(out);
os.write(0);
os.flush();
System.out.println(Files.exists(tmp));
os.close();
System.out.println(Files.exists(tmp));
}
}
On Windows, I see what I expect, i.e true false. On Linux I see false false. Is it expected? Am I doing something wrong?
The fact that the file is deleted too early is problematic since I need to test it for its size for instance after having written to it.
I use jdk7u25 on both Linux and Windows and could reproduce on machines with RedHat or ArchLinux on it.
EDIT: even if I test for file existence before another call to os.write() I am told the file does not exist anymore. If I open the file with the CREATE options, then I will see true true.
It looks like the Linux JVM deletes the file as soon as you open it, which makes sense as you can do that on Linux. That's how I would implement it too. You'll have to keep track of how much has been written to the file yourself, e.g. by interposing a FilterOutputStream that counts bytes.

Javascript to Java communication using LiveConnect not working

I've been working on a project that requires communication both directions between Java and JavaScript. I have successfully managed to get it working under all browsers in OS X, but I'm now faced with the challenge of getting it to run on Windows under any browser. At the moment it simply doesn't work.
I'm just wondering if there is something special I need to do in order for JavaScript to communicate with Java?
My applet code looks like this:
<applet id='theApplet'
code="com/company/MyApplet.class"
archive="SMyApplet.jar"
height="50" width="900"
mayscript="true" scriptable="yes">
Your browser is ignoring the applet tag.
</applet>
Once the applet has loaded, I then try to call functions on it like this:
alert("Call some java:" + theApplet.testFunc());
And in the firebug console I get the following error:
theApplet.testFunc is not a function
I can confirm that this doesn't work in IE either.
When the page loads, I have the java console open and I can see that the applet is successfully loading and ready to accept calls.
Any help would be greatly appreciated!
Cheers
Update: Here is the stripped down java code exposing the public api that I'm trying to call.
package com.company;
import com.google.gson.Gson;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.*;
import netscape.javascript.*;
public class MyApplet extends Applet implements Runnable
{
public void init()
{
JSON = new Gson();
isReadyVar = 0;
workThread = null;
}
public void start()
{
}
public void run()
{
System.out.println("Done");
}
public void stop()
{
}
public void destroy()
{
}
/* Public API */
public int testFunc()
{
return 200;
}
}
Update [SOLVED]:
I figured out what the problem was exactly. Turns out the Gson lib I was using wasn't signed; but my own jar was. Browsers on windows require that all libs are signed; so I packaged Gson in with my java files & signed the lot and it solved the problem! Thanks for everyones help!
I figured out what the problem was exactly. Turns out the Gson lib I was using wasn't signed; but my own jar was. Browsers on windows require that all libs are signed; so I packaged Gson in with my java files & signed the lot and it solved the problem! Thanks for everyones help!
alert("Call some java:" + document.getElementbyId("theApplet").testFunc());
Make sure the testFunc() method is declared as public access.
If that does not work, post the applet code as an SSCCE.
BTW
Incorrect
code="com/company/MyApplet.class"
Correct
code="com.company.MyApplet"
BTW 2
Incorrect
..scriptable="yes">
Correct
..scriptable="true">
Since the applet element is deprecated, I use following code, which works at least in Firefox:
<object id="MyApplet" classid="java:com.example.myapplet"
codetype="application/java" codebase="bin/" height="10" width="10"
</object>

Categories