I'm converting a project written with JRuby 1.7 in mind to JRuby 9.4. One of the files starts like this:
if RUBY_PLATFORM == 'java'
require 'java'
import java.lang.management.ManagementFactory
....
end
The import does not work anymore. With JRuby 9.4, just compiling this file produces the error message NoMethodError: undefined method `import' for main:Object.
My understanding was that require 'java' would provide the import method, and at least it was working with JRuby 1.7.
In case it matters: We are using the imported class like this:
current_heap_in_bytes = ManagementFactory.getMemoryMXBean.getHeapMemoryUsage.used
On this page:
https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby#from-class-files
There is mention of the java_import method. Does that help?
Related
If I made a python file named hello.py that has a script made like this.
msg = input("insert your message here: ")
script = '''
def say_something():
print("{msg}")
'''
exec(script)
say_something()
And then I tried to use Cython
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules=cythonize("Hello.py")
)
It will show an error like this: undeclared name not builtin: say_something
I do understand why this happens but I'm not really an expert with python and C just yet. This is just an example, but it's similar to what I'm trying to do with one of my projects. Is there any way I could resolve this? I want to find a way to convert the script string into C as well.
I was trying to build an editable python script.
Cython compiles the Python functions to a native binary that does what the CPython interpreter should do. exec is a function that execute arbitrary code at runtime (which is generally a very bad idea for speed, maintainability/readability and security). Cython does not support exec because it would mean that the could would be compiled at runtime. Thus, the code executed by exec cannot be a Cython code. However, the exec function can still be used to execute a pure-Python code. The error can be removed by turning off the Cython.Compiler.Options.error_on_unknown_names in the setup script (just before calling setup) as pointed out by #DavidW. With this Cython will not complain when it does not find a function defined by exec (or similar methods). Please keep in mind that CPython can only be used in this case instead of Cython (which partially defeat the purpose of using Cython in the first place).
I want to integrate a very simple Java file into my JRuby app. Unfortunately, it doesn't work.
Simple.java:
package com.mypackage;
public class Simple {
public void foo() {
System.out.println("foo called");
}
}
I compile it with "javac Simple.java"
Then I create a jar file with "jar cf mylib.jar Simple.class"
test.rb:
require 'java'
require "mylib.jar"
java_import 'com.mypackage.Simple'
When I run it with "ruby test.rb" I get the following error:
NameError: cannot load Java class com.mypackage.Simple
for_name at org/jruby/javasupport/JavaClass.java:1286
get_proxy_class at org/jruby/javasupport/JavaUtilities.java:34
I have no idea what I'm doing wrong.
My JRuby version is: jruby 1.7.16.1 (1.9.3p392)
The answer is simple. I have forgotten to put the Simple class into the folder structure com/mypackage/Simple. Java was therefore not able to find the class.
I bump version of junit to 4.11 and get:
[WARNING] [deprecation] Assert in junit.framework has been deprecated
[WARNING] [deprecation] Assert in junit.framework has been deprecated
....
How and to what migrate?
As it seems the Assert class has been moved from junit.framework to org.junit.Assert in JUnit 4.0 - you can use that instead, it's not deprecated.
Change your import statement from
import junit.framework.Assert;
to
import org.junit.Assert;
and this will rectify your JUnit deprecation warnings.
Both are depricated:
junit.framework.Assert.assertThat
org.junit.Assert.assertThat
According to docs, use Instead:
org.hamcrest.MatcherAssert.assertThat
After facing this problem I have tried lots of ways to solve this but failed again and again.
The good thing is: I have download junit-4.12.jar file from here and added the jar file in the project section under the libs folder. If previously any kind of Junit dependancy exist in the project then remove that from the build.gradle and build + clean your project.
It is worked for me. Hope it will work for you.
Note: Take a look in the image that I attached in below.
Thank you
We had a large number of tests with many assertions.
Adding something like
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
to the import statements also helped to limit the changes in test code.
You can refer to jUnit4 Assert class methods from JUnit4
Environment Details
Mac OS X 10.9
Oracle JDK 1.7.0_55 64-bit
jython-standalone-2.5.3.jar
junit-4.11
What I have done so far
I have added the junit jar to /Library/Java/Extensions.
I invoked Jython as follows java -jar jython-standalone-2.5.3.jar
In the Jython interpreter, I imported the following import org.junit.Assert, and this import was successful.
Problem
When I tried to use assertTrue, I got a NameError in the interpreter. Why is this so?
I understand that assertTrue is a static method. Not sure what implication this has when I try to use it in Jython.
Additional Context
I am using XMLUnit in Jython. Was able to successfully import the Diff class from org.custommonkey.xmlunit in Jython. Also able to use the methods in this class, and call them on a Diff object. The result of this method call is what I am trying to pass to assertTrue, when it throws the error.
from org.custommonkey.xmlunit import Diff
import org.junit.Assert
xml1 = ...some XML string...
xml2 = ...some XML string...
myDiff = Diff(xml1, xml2)
assertTrue(myDiff.similar())
Hope this additional information is useful in identifying a solution to this problem.
Latest Status
I narrowed it down to setting this property python.security.respectJavaAccessibility = false, since the Assert() constructor is protected.
Still trying to get it to work. Any help is greatly appreciated.
Figured it out.
In addition to junit.jar file, the hamcrest-core.jar file also needed to be copied to /Library/Java/Extensions.
Then I got rid of the jython.jar file, and instead installed it using the jython installer.
After the installation was completed, I updated the registry file in the installation folder, specifically setting this property python.security.respectJavaAccessibility = false.
Now I am able to see the assertTrue method, and no longer getting a NameError.
I am trying to execute some custom Java code through the latest version of Jruby (1.5.1), Ruby 1.8.7, with Java 1.6.0_06. I have tried both the class file and putting it in a jar method. When I try
require 'java'
require 'path_to_class/myClass
or
require 'java'
require 'path_to_jar/a_jar.jar
Trying both methods, I cannot access the myClass nor any other files in the jar file. Other variations on the net for importing java classes lead to the following error:
`NameError: cannot load Java class com.package.myClass from C:/jruby-1.5.1/lib/ruby/site_ruby/shared/builtin/javasupport/java.rb:51:in method_missing`
I have also checked the solutions on StackOverFlow and I still get the same outcome. I am wondering if this might be a problem at a deeper level.
Instead of 'require', you want 'java_import'.
require 'java'
java_import com.package.MyClass
See JRuby: import vs include vs java_import vs include_class for some more discussion e.g. why you should use 'java_import' instead of just 'import'
If you have a Java class com.mypackage.MyClass in the same folder, or in a folder present on the classpath, you can call it from your JRuby script like this:
require 'java'
import com.pack.MyClass
myClass = MyClass.new
If the class is in a jar, you have to require the jar:
require 'java'
require '/path/to/myjar.jar'
import com.pack.MyClass
myClass = MyClass.new
If myjar.jar is on the classpath, you can just use require 'myjar.jar'.
Did you try include Java?
See this for more details: http://blogs.oracle.com/coolstuff/entry/using_java_classes_in_jruby
So Here is what worked for me, I had all required stuff that people suggested but what I really needed was
$CLASSPATH << (Rails.root.to_s + "/path/to/dotClassFolder")
before the java_import statement
so in the file system, if your class was was in the folder
Rails.root/path/to/dotClassFolder/folder/anotherFolder/MyClass.class
Include $CLASSPATH << (Rails.root.to_s + "/path/to/dotClassFolder")
then java_import "folder.anotherFolder.MyClass"
See
From .class files section at https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby