You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am having issue when using ij.py.run_script to run java code. I was able to connect to my java code but in my java code, i import ij library, but the ij is not regconized. How can I fix this
My code is as below:
script_java="""import java.io.*;import ij.*;import ij.gui.*;import ij.plugin.*;import ij.io.*;import ij.text.*;import ij.util.*;import ij.process.*;import ij.measure.*;import java.nio.file.Path;import java.nio.file.Paths;import javax.xml.parsers.*;import org.w3c.dom.*;import java.net.URI;import org.xml.sax.*;public class CXMLFile {{ final String template = ""+ "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"no\\"?>"+ "<data_info>"+ "<volume type=\\"%s\\" width=\\"%d\\" height=\\"%d\\" stack=\\"%d\\" pitch=\\"%.4f\\" path=\\"%s\\"/>"+ "<position x=\\"0\\" y=\\"0\\" z=\\"0\\"/>"+ "<caption text=\\"%s\\"/>"+ "<original path=\\"\\"/>"+ "<range min=\\"%.4f\\" max=\\"%.4f\\"/>"+ "</data_info>"; private File file; public boolean loadfile (String file) {{ return loadfile(new File(file)); }} public boolean loadfile (File file) {{ this.file = file; return file.exists(); }} public String getAttribute(String tag1,String tag2) {{ String[] arguments = {{tag1,tag2}}; String result = ""; if (!this.file.exists()) {{ System.out.println(this.file.getAbsolutePath()); IJ.showMessage("file not found"); return ""; }} /* convert file path into URI for files on network disk. URI uri = fff.toURI() arguments[2] = uri.toString() File fff2 = new File(arguments[2]) */ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); try {{ DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); // encoding problem. & -> & File tmpFile = File.createTempFile("tmp",".xml"); // IJ.log("temp file is "+tmpFile.getAbsolutePath()); tmpFile.deleteOnExit(); PrintWriter pw = new PrintWriter(tmpFile,"UTF-8"); InputStreamReader fileReader = new InputStreamReader(new FileInputStream(this.file),"UTF-8"); BufferedReader bufferedReader = new BufferedReader(fileReader); String data; while ((data = bufferedReader.readLine()) != null) {{ // IJ.log("Read: "+data); data = data.replaceAll("&","&"); data = data.replaceAll("&amp;","&"); pw.println(data+"\\r\\n"); }} bufferedReader.close(); pw.close(); Document document = documentBuilder.parse(tmpFile); // document = documentBuilder.parse(arguments[2]); Element elementNode = document.getDocumentElement(); Node itemNodes = elementNode.getFirstChild(); while (itemNodes.getNodeName()!=arguments[0]) {{ itemNodes = itemNodes.getNextSibling(); }} if (itemNodes.hasChildNodes()) {{ Node subItemNodes = itemNodes.getFirstChild(); while (subItemNodes.getNodeName()!=arguments[1]) {{ subItemNodes = subItemNodes.getNextSibling(); }} result=subItemNodes.getTextContent(); }} else if (itemNodes.hasAttributes()) {{ NamedNodeMap subItemNodes=itemNodes.getAttributes(); Node item=subItemNodes.getNamedItem(arguments[1]); result=item.getNodeValue(); }} }} catch(ParserConfigurationException e) {{ IJ.showMessage("ParserConfiguration Error"); }} catch(FileNotFoundException e) {{ IJ.showMessage("File not found"); }} catch(UnsupportedEncodingException e) {{ IJ.showMessage("Unsupported Encoding"); }} catch(IOException e) {{ IJ.showMessage("File IO Error"); }} catch(SAXException e) {{ IJ.showMessage("Bad XML file"); }} catch (ArrayIndexOutOfBoundsException e) {{ IJ.showMessage("The tag do not exist"); }} return result; }} public double getAttributeDouble(String tag1,String tag2) {{ String att = getAttribute(tag1,tag2); Double parse = new Double(att); return parse.doubleValue(); }} public int getAttributeInt(String tag1,String tag2) {{ Double parse = new Double(getAttributeDouble(tag1,tag2)); return parse.intValue(); }} //}} String path = ""; // raw file name String caption = ""; // raw file name String directory = ""; String xmlfilename = ""; String inputfilepath = ""; String inputfilename = ""; public void Writer(ImagePlus imp, boolean saveraw) {{ String type = "byte"; Integer width = 2042; Integer height = 2042; Integer stack = 2048; Double pitch = 0.3500; Double min = 0.0; Double max = 255.0; if (imp == null) return; // width,height,stack int[] dim = imp.getDimensions(); width = dim[0]; height = dim[1]; stack = dim[3]; Calibration cal = imp.getCalibration(); // type, min, max if (imp.getType() == ImagePlus.GRAY16) {{ if (cal.isSigned16Bit()) {{ type = "short"; min = -32767.0; max = 32767.0; }} else {{ type = "word"; min = 1.0; max = 65535.0; }} }} else if (imp.getType() == ImagePlus.GRAY8) {{ type = "byte"; min = 1.0; max = 255.0; }} else {{ IJ.showMessage("unsupported type"); return; }} if (cal.getFunction() == Calibration.STRAIGHT_LINE) {{ double[] coef = cal.getCoefficients(); max = (max-min)*coef[1]+coef[0]+coef[1]; min = coef[0]+coef[1]; }} // pitch pitch = cal.pixelWidth; String unit = cal.getUnit(); if (unit.equals("mm")) pitch*=1000.0; if (unit.equals("cm")) pitch*=10000.0; if (unit.equals("inch")) pitch*=25400.0; if (unit.equals("nm")) pitch*=0.001; if (unit.equals("A")||unit.equals(IJ.angstromSymbol)) pitch*=0.0001; // path, caption is to be assigned later caption = imp.getShortTitle(); path = imp.getShortTitle()+".raw"; xmlfilename = caption+".xml"; if (!getSavePath()) return; String OutString = String.format(template,type,width,height,stack,pitch,path,caption,min,max); if (xmlfilename.isEmpty() || path.isEmpty()) {{ if (!getSavePath()) return; }} IJ.saveString(OutString,new File(directory,xmlfilename).toString()); if (saveraw) {{ IJ.run("Input/Output...","use_file save copy_column copy_row save_column save_row"); IJ.saveAs(imp,"raw",new File(directory,path).toString()); }} }} protected boolean getSavePath() {{ SaveDialog sd; if (directory.isEmpty()) {{ sd = new SaveDialog("Path of xml file is saved",path,".xml"); }} else {{ sd = new SaveDialog("Path of xml file is saved",directory,path,".xml"); }} if (sd.getFileName() == null || sd.getFileName().equals("")) return false; directory = sd.getDirectory(); xmlfilename = sd.getFileName(); path = xmlfilename.replaceAll(".xml","")+".raw"; caption = path; return true; }} public ImagePlus Reader() {{ return Reader(false); }} public ImagePlus Reader(boolean FlagVirtual){{ // OpenDialog od = new OpenDialog("Select an .xml file","","*.xml"); // if (od.getFileName()==null) return null; // File f = new File(od.getDirectory(), od.getFileName()); // String fn = od.getFileName(); File f = new File(inputfilepath, inputfilename); String fn = inputfilename; int pos = fn.lastIndexOf("."); if (pos>0) fn = fn.substring(0,pos); ImagePlus imp = null; //XMLParser xmlfile = new XMLParser(); //xmlfile.loadfile(f); loadfile(f); // String sPath = xmlfile.getAttribute("volume","path"); String sPath = getAttribute("volume","path"); if (sPath.toLowerCase().endsWith(".raw")) {{ // 20221209 RH FileInfo fi = new FileInfo(); //fi.directory = od.getDirectory(); fi.directory = inputfilepath; fi.fileName = fn+".raw"; fi.fileFormat = FileInfo.RAW; double range = 254; String type = getAttribute("volume","type"); if (type.equals("byte")||type.equals("8-bit Unsigned")) {{ fi.fileType = FileInfo.GRAY8; range = 255.0-1.0; }} else if (type.equals("short")||type.equals("16-bit Signed")) {{ fi.fileType = FileInfo.GRAY16_SIGNED; range = 32767.0-(-32767.0); }} else if (type.equals("word") || type.equals("16-bit Unsigned")) {{ fi.fileType = FileInfo.GRAY16_UNSIGNED; range = 65535.0-1.0; }} else {{ IJ.showMessage("Unsupported data type"); return null; }} // fi.width = xmlfile.getAttributeInt("volume","width"); // fi.height = xmlfile.getAttributeInt("volume","height"); // fi.nImages = xmlfile.getAttributeInt("volume","stack"); // double pitch = xmlfile.getAttributeDouble("volume","pitch"); fi.width = getAttributeInt("volume","width"); fi.height = getAttributeInt("volume","height"); fi.nImages = getAttributeInt("volume","stack"); double pitch = getAttributeDouble("volume","pitch"); fi.pixelWidth = pitch; fi.pixelHeight = pitch; fi.pixelDepth = pitch; fi.unit = "um"; fi.intelByteOrder = true; if (FlagVirtual) {{ VirtualStack fiv = new FileInfoVirtualStack(fi,false); imp = new ImagePlus(fn,fiv); }} else {{ System.out.println("TEST: " + fi.fileName); imp = new FileOpener(fi).open(false); }} // IJ.run(imp, "Properties...", String.format("unit=um pixel_width=%.4f pixel_height=%.4f voxel_depth=%.4f",pitch,pitch,pitch)); imp.setTitle(fn); String UniqueTitle = WindowManager.makeUniqueName(imp.getTitle()); imp.setTitle(UniqueTitle); // double dmin = xmlfile.getAttributeDouble("range","min"); // double dmax = xmlfile.getAttributeDouble("range","max"); double dmin = getAttributeDouble("range","min"); double dmax = getAttributeDouble("range","max"); double[] coef = {{0.0,1.0}}; coef[1] = (dmax-dmin)/range; coef[0] = dmin-coef[1]; Calibration cal = imp.getCalibration(); cal.setFunction(Calibration.STRAIGHT_LINE,coef,"Gray Value"); cal.setUnit("um"); cal.pixelWidth = pitch; cal.pixelHeight = pitch; cal.pixelDepth = pitch; }} return imp; }}; public static void main(String[] args) {{ try {{ System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); Path currentRelativePath = Paths.get(""); String s = currentRelativePath.toAbsolutePath().toString(); System.out.println("Current absolute path is: " + s); CXMLFile XMLFile = new CXMLFile(); XMLFile.inputfilepath = "{0}"; XMLFile.inputfilename = "{1}"; ImagePlus imp = XMLFile.Reader(); // imp.show(); return "TEST"; }} catch (Exception e) {{ System.out.println(e); e.printStackTrace(); }} }}}}"""importimagejij=imagej.init("2.5.0", mode='interactive')
print(f"ImageJ version: {{ij.getVersion()}}")
file_path="path\\to\\folder"file_name="file\\name"script_java=script_java.format(file_path, file_name)
result=ij.py.run_script("java", script_java)
print(result)
My error is as below:
CXMLFile
not up-to-date because 1 source files are not up-to-date (C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java)[java.lang.Enum.toString]
CXMLFile
not up-to-date because 1 source files are not up-to-date (C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java)
[java.lang.Enum.toString]
Compiling 1 file in C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028
[java.lang.Enum.toString] Compiling 1 file in C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028[java.lang.Enum.toString]
[-classpath, C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\target\classes, -d, C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\target\classes, C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java]
[java.lang.Enum.toString] [-classpath, C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\target\classes, -d, C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\target\classes, C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java][java.lang.Enum.toString]
using the class path: C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\target\classes
[java.lang.Enum.toString] using the class path: C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\target\classes[java.lang.Enum.toString]
警告:注釈プロセッサ'org.scijava.annotations.AnnotationProcessor'から-source '11'より小さいソース・バージョン'RELEASE_8' がサポートされています
[java.lang.Enum.toString] 警告:注釈プロセッサ'org.scijava.annotations.AnnotationProcessor'から-source '11'より小さいソース・バージョン'RELEASE_8'がサポートされています
C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:136: エラー: シンボルを見つけられません
[java.lang.Enum.toString] C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:136: エラー: シンボルを見つけられません
public void Writer(ImagePlus imp, boolean saveraw) {
[java.lang.Enum.toString] public void Writer(ImagePlus imp, boolean saveraw) {
^
[java.lang.Enum.toString] ^
シンボル: クラス ImagePlus
[java.lang.Enum.toString] シンボル: クラス ImagePlus
場所: クラス CXMLFile
[java.lang.Enum.toString] 場所: クラス CXMLFile
C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:221: エラー: シンボルを見つけられません
[java.lang.Enum.toString] C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:221: エラー: シンボルを見つけられません
public ImagePlus Reader() {
[java.lang.Enum.toString] public ImagePlus Reader() {
^
[java.lang.Enum.toString] ^
シンボル: クラス ImagePlus
[java.lang.Enum.toString] シンボル: クラス ImagePlus
場所: クラス CXMLFile
[java.lang.Enum.toString] 場所: クラス CXMLFile
C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:225: エラー: シンボルを見つけられません
[java.lang.Enum.toString] C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:225: エラー: シンボルを見つけられません
public ImagePlus Reader(boolean FlagVirtual){
[java.lang.Enum.toString] public ImagePlus Reader(boolean FlagVirtual){
^
[java.lang.Enum.toString] ^
シンボル: クラス ImagePlus
[java.lang.Enum.toString] シンボル: クラス ImagePlus
場所: クラス CXMLFile
[java.lang.Enum.toString] 場所: クラス CXMLFile
C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:2: エラー: パッケージijは存在しません
[java.lang.Enum.toString] C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:2: エラー: パッケージijは存在しません
import ij.*;
[java.lang.Enum.toString] import ij.*;
^
[java.lang.Enum.toString] ^
C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:3: エラー: パッケージij.guiは存在しません
[java.lang.Enum.toString] C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:3: エラー: パッケージij.guiは存在しません
import ij.gui.*;
[java.lang.Enum.toString] import ij.gui.*;
^
[java.lang.Enum.toString] ^
C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:4: エラー: パッケージij.pluginは存在しません
[java.lang.Enum.toString] C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:4: エラー: パッケージij.pluginは存在しません
import ij.plugin.*;
[java.lang.Enum.toString] import ij.plugin.*;
^
[java.lang.Enum.toString] ^
C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:5: エラー: パッケージij.io は存在しません
[java.lang.Enum.toString] C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:5: エラー: パッケージij.ioは存在しません
import ij.io.*;
[java.lang.Enum.toString] import ij.io.*;
^
[java.lang.Enum.toString] ^
C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:6: エラー: パッケージij.textは存在しません
[java.lang.Enum.toString] C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:6: エラー: パッケージij.textは存在しません
import ij.text.*;
[java.lang.Enum.toString] import ij.text.*;
^
[java.lang.Enum.toString] ^
C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:7: エラー: パッケージij.utilは存在しません
[java.lang.Enum.toString] C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:7: エラー: パッケージij.utilは存在しません
import ij.util.*;
[java.lang.Enum.toString] import ij.util.*;
^
[java.lang.Enum.toString] ^
C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:8: エラー: パッケージij.processは存在しません
[java.lang.Enum.toString] C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:8: エラー: パッケージij.processは存在しません
import ij.process.*;
[java.lang.Enum.toString] import ij.process.*;
^
[java.lang.Enum.toString] ^
C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:9: エラー: パッケージij.measureは存在しません
[java.lang.Enum.toString] C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\src\main\java\CXMLFile.java:9: エラー: パッケージij.measureは存在しません
import ij.measure.*;
[java.lang.Enum.toString] import ij.measure.*;
^
[java.lang.Enum.toString] ^
エラー11個
[java.lang.Enum.toString] エラー11個
警告1個
[java.lang.Enum.toString] 警告1個
[ERROR] java.io.FileNotFoundException: C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\target\classes\META-INF\maven\
org.scijava.scripting.java
\
CXMLFile
\pom.xml (ファイル名、ディレクトリ名、またはボリューム ラベルの構文が間違っています。)
at java.base/java.io.FileOutputStream.open0(Native Method)
at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:187)
at org.scijava.minimaven.BuildEnvironment.copy(BuildEnvironment.java:631)
at org.scijava.minimaven.BuildEnvironment.copyFile(BuildEnvironment.java:625)
at org.scijava.minimaven.MavenProject.build(MavenProject.java:548)
at org.scijava.minimaven.MavenProject.build(MavenProject.java:463)
at org.scijava.minimaven.MavenProject.build(MavenProject.java:446)
at org.scijava.plugins.scripting.java.JavaEngine.compile(JavaEngine.java:209)
at org.scijava.plugins.scripting.java.JavaEngine.eval(JavaEngine.java:136)
at org.scijava.script.ScriptModule.run(ScriptModule.java:157)
at org.scijava.module.ModuleRunner.run(ModuleRunner.java:163)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:124)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:63)
at org.scijava.thread.DefaultThreadService.lambda$wrap$2(DefaultThreadService.java:225)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
[java.lang.Enum.toString] [ERROR] java.io.FileNotFoundException: C:\Users\KIENCT~1.ONS\AppData\Local\Temp\java9485459129006429028\target\classes\META-INF\maven\
org.scijava.scripting.java
\
CXMLFile
\pom.xml (ファイル名、ディレクトリ名、またはボリューム ラベルの構文が間違っています。)
at java.base/java.io.FileOutputStream.open0(Native Method)
at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:187)
at org.scijava.minimaven.BuildEnvironment.copy(BuildEnvironment.java:631)
at org.scijava.minimaven.BuildEnvironment.copyFile(BuildEnvironment.java:625)
at org.scijava.minimaven.MavenProject.build(MavenProject.java:548)
at org.scijava.minimaven.MavenProject.build(MavenProject.java:463)
at org.scijava.minimaven.MavenProject.build(MavenProject.java:446)
at org.scijava.plugins.scripting.java.JavaEngine.compile(JavaEngine.java:209)
at org.scijava.plugins.scripting.java.JavaEngine.eval(JavaEngine.java:136)
at org.scijava.script.ScriptModule.run(ScriptModule.java:157)
at org.scijava.module.ModuleRunner.run(ModuleRunner.java:163)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:124)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:63)
at org.scijava.thread.DefaultThreadService.lambda$wrap$2(DefaultThreadService.java:225)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
org.scijava.script.ScriptModule@52fbe6ee
The text was updated successfully, but these errors were encountered:
@chautuankien Hmm, I haven't tried using the Java script runner (which compiles the code on the fly) from PyImageJ before. It wouldn't surprise me if it just does not work properly. That script engine is a bit finicky. You could try running that code as BeanShell (no changes) or Groovy (very small or no changes typically required) instead.
I also suggest to double check you are using the latest release of PyImageJ, and update the "2.5.0" version of ImageJ2 to "2.13.1", the most recent release as of this writing.
I am having issue when using
ij.py.run_script
to run java code. I was able to connect to my java code but in my java code, i import ij library, but the ij is not regconized. How can I fix thisMy code is as below:
My error is as below:
The text was updated successfully, but these errors were encountered: