Вопрос

I have a java program that calls a R script. Here is the command:

String filename = "hierarchicalClusteringScript.R";
String[] cmd=new String[]{this.conn.getRPath(),"CMD","BATCH",
            "--args LD_matrix='"+LD_matrix+"' " +
            "image_filename='"+image_filename+"' " +
            "width="+300+" "+
            "height="+height+" "+
            "ordered_snps_filename='"+ordered_snps_filename+"'",
            filename,
               this.conn.getWorkingDir()+this.conn.getProjectName()+"Routput.txt"};
    Runtime runtime = Runtime.getRuntime();
    try {
        Process process = runtime.exec(cmd);
        process.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

I would like to include the R script "hierarchicalClusteringScript.R" into the jar file. In case I do is there a way to call the script from code? Which path should I use?

Thanks a lot in advance

Это было полезно?

Решение

In order to extract the script from your JAR, normally we seldom perform a JAR/ZIP extract. When you put the script in the JAR, it usually implies that it is in your classpath. Therefore, just locate it as a resource, get the inputStream to it, and read the resources and write it to a temp directory. These few steps should be easy enough that you can easily find code sample from the net easily.

some hints for you:

Class#getResourceAsStream()
FileOutputStream
File#createTempFile()

Другие советы

Jar files are essentially Zip files (with a Manifest).

You can extract files out of these using the Jar API

Essentially, you want to open the JarFile, find the JarEntry and using the JarOutputStream extract the contents

You can have a look at How to write a Java program which can extract a JAR file and store its data in specified directory (location)? for an example

If I understand you are looking to add R script to jar and load R as a resource from jar classpath. (jar can contain any types of files, but JVM can only run what is bytecode compatible)

-- To add script to existing jar do this:
http://docs.oracle.com/javase/tutorial/deployment/jar/update.html

-- To create new jar with script do this:
http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

-- To load the script from jar make sure it is in a classpath and in your code load it with ResourceLoader:
http://docs.oracle.com/javase/1.5.0/docs/guide/lang/resources.html

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top