aspose file tools*
The moose likes Beginning Java and the fly likes Using my own types on a Script Engine.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using my own types on a Script Engine...." Watch "Using my own types on a Script Engine...." New topic
Author

Using my own types on a Script Engine....

Vinicius Souza
Ranch Hand

Joined: May 18, 2011
Posts: 45

Hi ppls how are you doing?
Im trying to expose some of my own types using the 'put' method from the java Script Engine but its guive-me a error. See my code:

See, the Actor class is a custom class of mine so when I try to use getY() method java return an error tell me that the method does not exists. Whats wrong ? Can I use my own object or only the java native ones?
Thanks in advance!


Java is the best (I love C too) heehhh
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

I tried your code. I had to make up my own class Actor because you didn't post yours. It worked without any errors and printed 200.

Does your class Actor have a public method called getY?

Here's my Actor class, very simple:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vinicius Souza
Ranch Hand

Joined: May 18, 2011
Posts: 45

Its weird because my Actor class is on the same package com.xcretor and the method getX() is public. Im getting this error:
"sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function getY in object com.xcreator.Actor@53187f60. (<Unknown source>#1) in <Unknown source> at line number 1"
Its really weird....
Here is my actor class:

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Vinicius Souza wrote:Its weird because my Actor class is on the same package com.xcretor and the method getX() is public.

Yes, but this is about the getY method, not the getX method.

And looking at your code, the getX and getY methods are not public, they have default access.

You're writing in a kind of C++-style. Specifying access specifiers in Java does not work the same as in C++. Look at this:


Do you understand what's happening here? Only the variable x has private access. All the other variables have default access. The indentation you've used is misleading - as if all the variables are private. This is the same as:


You are doing the same with the constructor and methods:

The constructor is public, but all the methods have default access.

You have to specify private or public for all variables and methods separately.
Vinicius Souza
Ranch Hand

Joined: May 18, 2011
Posts: 45

Hummm, thanks a lot friend. Solved my problem! I really was thinking like a C programmer ehehehehe.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using my own types on a Script Engine....
 
Similar Threads
Exception in thread "main" NoClassDefFoundError
Would like to use the ScriptEngineManager in Netbeans
Where do I get java-engine.jar to make new ScriptEngineManager().getEngineByName("java"); work
Calling Javascript function from Java Code
Runtime Java Expression Evaluator