Add java class to jython as jython import module - java

I am using jython interpreter programmatically (PythonInterpreter) in my java application, and I'd like to add a specific class, eg. MyJythonLib as a Jython import module so that my Jython script could have something like:
import MyJythonLib
a = MyJythonLib.getStuff()
where MyJythonLib.java is
public class MyJythonLib
{
private Object stuff;
public Object getStuff()
{
return stuff;
}
}
How do I set this class to be a module for my Jython interpreter?

Ensure MyJythonLib is in your classpath.
import MyJythonLib with reference to its fully qualified name.
For example:
import com.mycompany.MyJythonLib
Or:
from com.mycompany import MyJythonLib
Or:
import com.mycompany.MyJythonLib as MyJythonLib

Related

Is their any class/classes inside java root package

The below code has no compilation error:-
import java.*;
class Test{
public static void main(String[] args){
}
}
My question is does the package named java only includes sub-packages or it also includes any class/classes. If yes then which class(s). If no then why we are able to import it.
There are no class directly under java. All the JDK's classes are under subpackages.
Having an empty package (or a package with no classeses in it) is perfectly legal in Java. You can import all the classes in it (which is no classes) with the * syntax. This isn't wrong - it's just pointless.

Where am I going wrong in trying to import a class?

Here's the code for the source file:
package moa4;
public class Book {
....
}
And for the destination file:
import moa4.Book;
public class Library {
...
}
The source and the destination are both saved in the same directory with the address:
C:\Users\\java\M\moa4
I'm getting the following error: package moa4 does not exist
You asked Library to import a package moa4.Book but you defined no such package. Instead, you defined a type Book inside package moa4, and that is not consistent with your import directive.
You could either import the package, or make that an import static of the class, but since Book and Library are both in the same package you don't need the import directive at all.
As mentioned, C:/Users/java/M needs to be in your classpath ("-cp" option).

eclipse: import (classname) cannot be resolved

I am new in using eclipse java using multiple .java files. My eclipse java project consist of one project file two package files, each with one .java class
My 2nd java class import the 1st java class/package, like so
VerifyLogin.java
package VerifyLogin;
import ArgumentCountException;
ArgumentCountException.java
// ...
The problem is VerifyLogin.java is getting an error
Import ArgumentCountException cannot be resolved
Or any reference I have to ArgumentCountException cannot be resolved to a type.
In java if you need to import a class then you need to use the full qualified name for that class, as the following:
import packageName.YourClass;
For Example, if your need to use Scanner class, then you need to import it as:
import java.util.Scanner;
But if the class was withing the same package, you don't need to import it.
When importing your class, it should be done as below:
//Current package name for the VerifyLogin Class (All package names should be lowercase by convention)
package packageforcurrentclass;
//Import statements: import thedependencyclasspackage.thedependencyclassname
import exceptionpackage.ArgumentCountException;
public class VerifyLogin
{
...
}

Same package name in both Java and Python

In Jython, we can easily import a Java package like this:
Java:
package javapkg;
public class TestClassInJava {
}
Python:
from javapkg import TestClassInJava
However, if we have another package with the same name in Python:
javapkg/__init__.py:
class AnotherClassInPython:
pass
And in another Python file:
from javapkg import AnotherClassInPython # success
from javapkg import TestClassInJava # ImportError: No module named TestClassInJava
The Python package can be successfully imported, while the Java class in the same package will unable to be imported.
Is there anyway to merge these two namespace to make them both available?

How to extend a protected method in Clojure

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import clojure.lang.RT;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;
public class Clojure4Vaadin extends AbstractApplicationServlet {
#Override
protected Class<? extends Application> getApplicationClass()throws ClassNotFoundException {
return Application.class;
}
.... Some code .....
}
How to write this in Clojure?
I'm trying to write the vaadin srvlet class in clojure:
http://dev.vaadin.com/wiki/Articles/ClojureScripting
I think what you're looking for is the following:
(def Clojure4Vaadin
(proxy [com.vaadin.terminal.gwt.server.AbstractApplicationServlet] []
(getApplicationClass [] com.vaadin.Application)))
Have a look at the documentation of proxy.
The code you have given above is used to serve a webapp written in clojure via the vaadin framework.
This code is meant to be run as Java Servlet as it is and the webapp logic would be in the clojure code (test.tlp), you would have to compile the servlet and package it with the clojure script in the webapp root directory.
Regards,
Shanmu

Categories