简体   繁体   English

从jar文件执行时JRuby文件无法运行(以前是:System.out.println无法从jar文件运行)

[英]JRuby file not run when executing from jar file (was: System.out.println not working from jar file)

(Crossposted at JRuby Forum , but posted here again because I did not get any answer yet). (在JRuby论坛上交叉发布,但由于我尚未得到任何答案而再次发布在这里)。

Platform: jruby 9.0.4.0 (2.2.2) 2015-11-12 b9fb7aa Java HotSpot(TM) 64-Bit Server VM 24.79-b02 on 1.7.0_79-b15 +jit [Windows 7-amd64] 平台:jruby 9.0.4.0(2.2.2)2015-11-12 b9fb7aa Java HotSpot(TM)64位服务器VM 1.7.0_79-b15 + jit上的24.79-b02 [Windows 7-amd64]

Main program in Java, calling JRuby code via RedBridge Core. Java主程序,通过RedBridge Core调用JRuby代码。 Java classes are in a Jar-File. Java类位于Jar文件中。

This setup works, as long I don't insist that the Ruby code is also executed from within the Jar-File, instead of being searched inside the file system. 只要我不坚持认为Ruby代码也是从Jar文件中执行的,而不是在文件系统中进行搜索,则此设置有效。 If I run the code from a Jar-File, the JRuby stdout seems to have disappeared. 如果我从Jar文件运行代码,则JRuby stdout似乎已消失。

First, here is the WORKING case: 首先,这是工作情况:

// My main program (Jmain.java):
import vp.VP;
public class Jmain {
  public static void main(String[] args){
    System.out.println("Jmain started");
    VP vp = new vp.VP();
    System.out.println("vp instance created");
    vp.run();
    System.out.println("Jmain terminating");
  }
}

// My VP class (VP.java):
package vp;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.RubyObject; 
import org.jruby.embed.ScriptingContainer;
import org.jruby.embed.LocalContextScope;
import java.util.*;
public class VP {
  private ScriptingContainer container;
  public VP() {
    container = new ScriptingContainer(LocalContextScope.SINGLETHREAD);
  }
  public void run() {
    RubyObject asahi =
        (RubyObject)container.runScriptlet( 
            org.jruby.embed.PathType.RELATIVE,"rbsrc/bridge.rb");
    System.out.println("scriptlet executed");
  }
}

# My Ruby program rbsrc/bridge.rb
puts "Entering bridge" # Just to see what's going on
File.write("out.txt","This file was created by bridge.rb\n")
# ... rest of the code not shown

:: I put everything into a jar file, including the Ruby files
:: (although I don't need them there yet).
jar cvfm jars\vp.jar .... rbsrc

:: The program is executed like this:
java -cp c:\jruby-9.0.4.0\lib\jruby.jar;jars\vp.jar Jmain

I see the result of all the println statements, and the file out.txt is created. 我看到了所有println语句的结果,并创建了文件out.txt。

Now for the NON-WORKING case: 现在,对于非工作情况:

In the whole setting, I change ONLY ONE line: The invocation of bridge.rb becomes 在整个设置中,我仅更改一行:bridge.rb的调用变为

RubyObject asahi = 
    (RubyObject)container.runScriptlet(
         org.jruby.embed.PathType.CLASSPATH,"bridge.rb");

That is, I replace RELATIVE by CLASSPATH, and drop the "rbsrc/". 也就是说,我将CLASSPATH替换为RELATIVE,然后删除“ rbsrc /”。 When I run this, I get no error message, I get the output of all the println statements, BUT I don't see the output of the 'puts' statement, nor is the file out.txt created! 当我运行此命令时,没有收到错误消息,没有得到所有println语句的输出,但是我看不到“ puts”语句的输出,也没有创建文件out.txt!

Note that bridge.rb seems to be loaded correctly (if I change bridge.rb to a different name, I get an exception), but it doesn't seem to be executed. 请注意,bridge.rb似乎已正确加载(如果将bridge.rb更改为其他名称,则会出现异常),但似乎未执行。

How come? 怎么会?

UPDATE : My problem description was wrong! 更新 :我的问题描述不对! There is nothing wrong with stdout, but it seems that my JRuby code bridge.rb is not executed! stdout没什么问题,但是似乎我的JRuby代码bridge.rb没有执行! runScriptlet returns null , and when I create a file inside the Ruby program, there is no file afterwards. runScriptlet返回null ,并且当我在Ruby程序中创建文件时,此后没有文件。

What am I doing wrong? 我究竟做错了什么? I have all my JRuby files in one directory. 我所有的JRuby文件都放在一个目录中。 I put them into my Jar file. 我将它们放入Jar文件中。 I run it using jar -cp JARFILE MAINCLASS . 我使用jar -cp JARFILE MAINCLASS运行它。 What else is missing? 还缺少什么?

BTW, the whole example application can be found here . 顺便说一句,整个示例应用程序可以在这里找到。 There is a readme.txt included. 包含一个readme.txt

You said 你说

I replace RELATIVE by CLASSPATH 我用CLASSPATH代替RELATIVE

You probably changed the setting where java should search for classes from a relative path to the CLASSPATH. 您可能更改了Java应该在从相对路径到CLASSPATH的类中搜索类的设置。

You probably have not set the correct CLASSPATH variable value for your jar files - meaning the place where java should check for extra classes ( more ). 您可能没有为jar文件设置正确的CLASSPATH变量值-意味着java应该在其中检查额外的类( 更多 )。

Try 尝试

set classpath = .;yourFilename.jar;

Notice that the dot (.) stands for the current directory. 请注意,点(。)代表当前目录。

You can then attempt to execute it using the -jar parameter in your command prompt: 然后,您可以在命令提示符下使用-jar参数尝试执行它:

java -jar yourFilename.jar

If you don't have a manifest in your jar, attempt executing it using the -cp (or -classpath, which is identical) parameter to specify your class path implicitly. 如果jar中没有清单,请尝试使用-cp(或-classpath,相同)参数执行该清单,以隐式指定您的类路径。

java -cp foo.jar full.package.name.MyClassName

Now, as a second thought, I would check that the executing directory has the permissions for the jar execution and the file creation (write), just in case. 现在,作为第二个想法,我将检查执行目录是否具有执行jar和文件创建(写)的权限,以防万一。

UPDATE: It seems out by what you describe, that the problem is finally not in the classpath - as you don't have any exceptions, but in the path you have set for the file. 更新:从您的描述看来,问题最终不在类路径中,因为您没有任何例外,但是在您为文件设置的路径中。

I suggest that you check the relative path you have set for the file inside your application that has a correct reference (just the file name is not enough, as "rbsrc/bridge.rb" is a relative path inside the application folders and this should be kept even when changed the option from RELATIVE to CLASSPATH). 我建议您检查为应用程序中具有正确引用的文件设置的相对路径(仅文件名不够,因为“ rbsrc / bridge.rb”是应用程序文件夹中的相对路径,因此应该即使将选项从RELATIVE更改为CLASSPATH也要保留)。 The relative path is based on the application folder as root in that case. 在这种情况下,相对路径基于作为根的应用程序文件夹。

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

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