[英]How to call a Java program from a Clojure program ?
package javaCodes;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class interm3parse {
public static boolean checkGroup(String str)throws IOException
{
FileInputStream fis = new FileInputStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while((line=br.readLine())!=null)
{
if(line.contains(str))
{
return true;
}
}
br.close();
return false;
}
public static boolean checkPresence(String str) throws IOException
{
FileInputStream fis = new FileInputStream(filepath);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while((line=br.readLine())!=null)
{
if(line.startsWith(str))
{
return true;
}
}
br.close();
return false;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
PrintWriter pw = new PrintWriter(new FileWriter("filepath"));
FileInputStream fis = new FileInputStream("filepath");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
StringBuilder sb = new StringBuilder();
String s=new String();
while ((line = br.readLine()) != null)
{
s="";
if(!line.startsWith("defmeasure")||(line==null))
{
String[] splitArray = line.split("\\s");
s=s+splitArray[0];
//System.out.println(splitArray[0]);
for(int i=1;i<splitArray.length;i++)
{
if(splitArray[i].contains("/"))
{
String[] alias = splitArray[i].split("/");
if(checkPresence(alias[1])||checkGroup(alias[1]))
{
s=s+" "+alias[1];
}
}
else
{
s=s+" "+splitArray[i];
}
}
pw.write(s);
pw.println();
System.out.println(s);
}
else
{
//fw.write(line);
//fw.write("\n");
System.out.println(line);
pw.write(line);
pw.println();
}
}
pw.flush();
pw.close();
}//main
}//end of class
I have a user defined Java program (interm3parse.java) ;I want to call that program(functions) from clojure program ....Both the codes are working fine individually ..Java code is in javaCodes package........................clojure code is in anothernamespace................................. 我有一个用户定义的Java程序(interm3parse.java);我想从clojure程序中调用该程序(函数)....两个代码都可以单独正常工作..Java代码在javaCodes包中... ......... clojure代码在另一个命名空间中........................... ......
Clojure code
(ns javaCodes.generalizedmeasuretocomp )
(defn output-Measure-Criterias-Final []
;;;Java program to replace all group components and remove aliases
)
You can call Java code from Clojure (assuming Java code classes are on Clojure application's classpath): 您可以从Clojure调用Java代码 (假设Java代码类位于Clojure应用程序的类路径中):
As your Java class defines static methods you can call them like that: 当Java类定义静态方法时,您可以这样调用它们:
(ns javaCodes.generalizedmeasuretocomp
(:import javaCodes.interm3parse))
(defn output-Measure-Criterias-Final []
(javaCodes.interm3parse/checkGroup "some string"))
By the way I noticed that your Java code might not work correcly (but I guess it is only for illustration purposes). 顺便说一句,我注意到您的Java代码可能无法正确运行(但我想它仅是出于说明目的)。 It uses some undefined names (eg
filepath
) and it might leak resources (when it returns true
it won't close the input stream). 它使用一些未定义的名称(例如
filepath
),并且可能会泄漏资源(返回true
时,它不会关闭输入流)。
I think you should wrap your resources into try-with-resources
statement, or write that code in Clojure using with-open
: 我认为您应该将资源包装到
try-with-resources
语句中,或者使用with-open
在Clojure中编写该代码:
(defn file-line-starts-with-str? [filename s]
(with-open [reader (clojure.java.io/reader filename)]
(->> reader
(line-seq)
(some #(clojure.string/starts-with? % s)))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.