简体   繁体   English

Aspose with RJB(Ruby Java Bridge)无法正常工作

[英]Aspose with RJB (Ruby Java Bridge) is not working

I have a code in Java that opens a excel template by aspose library (it runs perfectly): 我有一个Java代码,可以通过aspose库打开一个excel模板(它可以完美运行):

import com.aspose.cells.*;
import java.io.*;

public class test
{
    public static void main(String[] args) throws Exception
    {
        System.setProperty("java.awt.headless", "true");
        FileInputStream fstream = new FileInputStream("/home/vmlellis/Testes/aspose-cells/template.xlsx");
        Workbook workbook = new Workbook(fstream);
        workbook.save("final.xlsx");
    }
}

After I run this on Ruby with RJB (Ruby Java Bridge): 在使用RJB (Ruby Java Bridge)在Ruby上运行此代码之后:

require 'rjb'

#RJM Loading
JARS = Dir.glob('./jars/*.jar').join(':')
print JARS
Rjb::load(JARS, ['-Xmx512M'])

system = Rjb::import('java.lang.System')
file_input = Rjb::import('java.io.File')
file_input_stream = Rjb::import('java.io.FileInputStream')
workbook = Rjb::import('com.aspose.cells.Workbook')

system.setProperty("java.awt.headless", "true")
file_path = "/home/vmlellis/Testes/aspose-cells/template.xlsx"
file = file_input.new(file_path)
fin = file_input_stream.new(file)

wb = workbook.new(fin)

I get this error: 我收到此错误:

test.rb:57:in `new': Can't find file: java.io.FileInputStream@693a317a. (FileNotFoundException)
    from aspose-test.rb:57:in `<main>'

Why? 为什么? I run the same code... but in Ruby is not working! 我运行相同的代码...但是在Ruby中不起作用! How do I fix this? 我该如何解决?

Update: 更新:

In documentation there is the the initializer: Workbook(java.io.InputStreamstream)... but it's not working in RJB. 文档中有一个初始化程序:Workbook(java.io.InputStreamstream)...但在RJB中不起作用。 (How is this possible?) (这怎么可能?)

Your program should have worked, but I could not find any reason why it didn't and I am looking into it. 您的程序应该可以运行,但是我找不到任何原因,所以我正在调查。

Now the alternate approaches. 现在,替代方法。

Approach 1 Use Workbook(String) constructor instead of Workbook(FileInputStream). 方法1使用Workbook(String)构造函数而不是Workbook(FileInputStream)。 This worked flawlessly at my end. 这在我最后完美无瑕。 The sample code is 示例代码是

require 'rjb'

#RJM Loading
JARS = Dir.glob('/home/saqib/cellslib/*.jar').join(':')
print JARS
Rjb::load(JARS, ['-Xmx512M'])

system = Rjb::import('java.lang.System')
workbook = Rjb::import('com.aspose.cells.Workbook')

system.setProperty("java.awt.headless", "true")
file_path = "/home/saqib/rjb/template.xlsx"
save_path = "/home/saqib/rjb/final.xlsx"

wb = workbook.new(file_path)
wb.save(save_path)

Approach 2 Write a new Java class library. 方法2编写一个新的Java类库。 Write all your Aspose.Cells related code in it. 在其中编写所有与Aspose.Cells相关的代码。 Expose very simple and basic methods that needs to be called from Ruby (RJB). 公开需要从Ruby(RJB)调用的非常简单和基本的方法。 Why? 为什么?

  • It is easy to write program in native Java language. 用本机Java语言编写程序很容易。 If you use RJB, you need to perform a lot of code conversions 如果使用RJB,则需要执行很多代码转换
  • It is easy to debug and test in Java. 使用Java进行调试和测试很容易。
  • Usage of RJB will only be limited to calling methods from your own Java library. RJB的用法仅限于从您自己的Java库中调用方法。 The RJB code will be small and basic. RJB代码小而基本。

Similar Example using own library Create a new Java project, lets say "cellstest". 使用自己的库的类似示例创建一个新的Java项目,说“ cellstest”。 Add a new public class in it. 在其中添加一个新的公共类。

package cellstest;
import com.aspose.cells.Workbook;
public class AsposeCellsUtil 
{
    public String doSomeOpOnWorkbook(String inFile, String outFile)
    {
        String result = "";
        try
        {
            // Load the workbook
            Workbook wb = new Workbook(inFile);
            // Do some operation with this workbook
            // ..................
            // Save the workbook
            wb.save(outFile);
            // everything ok.
            result = "ok";
        }
        catch(Exception ex)
        {
            // Return the exception to calling program
            result = ex.toString();
        }
        return result;
    }
}

Like this, add as many methods as you like, for each operation. 这样,为每个操作添加尽可能多的方法。 Build the project and copy the "cellstest.jar" in same folder where you copied Aspose.Cells jar files. 生成项目,并将“ cellstest.jar”复制到复制Aspose.Cells jar文件的文件夹中。 You can return a String from your methods and check the return value in Ruby program for success or error code. 您可以从方法中返回String,然后在Ruby程序中检查返回值是否成功或错误代码。 The Ruby program will now be like Ruby程序现在就像

require 'rjb'

#RJM Loading
JARS = Dir.glob('/home/saqib/cellslib/*.jar').join(':')
print JARS
Rjb::load(JARS, ['-Xmx512M'])

system = Rjb::import('java.lang.System')
AsposeCellsUtil = Rjb::import('cellstest.AsposeCellsUtil')

system.setProperty("java.awt.headless", "true")
file_path = "/home/saqib/rjb/template.xlsx"
save_path = "/home/saqib/rjb/final.xlsx"

# initialize instance
asposeCellsUtil = AsposeCellsUtil.new()
# call methods
result = asposeCellsUtil.doSomeOpOnWorkbook(file_path, save_path)

puts result

PS. PS。 I work for Aspose as Developer Evangelist. 我为Aspose工作,担任开发人员推广人员。

In your Java code, you pass a file name string into FileInputStream() constructor: 在Java代码中,您将文件名字符串传递给FileInputStream()构造函数:

FileInputStream fstream = new FileInputStream("/home/vmlellis/Testes/aspose-cells/template.xlsx");

In your Ruby code, you pass a file object: 在您的Ruby代码中,您传递了一个文件对象:

file = file_input.new(file_path)
fin = file_input_stream.new(file)

Have you tried to do the same thing as in Java? 您是否尝试过执行与Java中相同的操作?

fin = file_input_stream.new(file_path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM