I wanted to import a java package "TestPackage" into jython script,
import TestPackage
print TestPackage.SampleClass().getMessage();
I am getting the following error,
ImportError: No module named TestPackage","errorPoint":"","lineNumber":"1","errorPointMessage":"in <module>\nImportError: No module named TestPackage
When I use from TestPackage import SampleClass works fine. If I execute from <pacakage> import <anyclass> statement once, then import <pacakage> works without the import error.
I tried setting PySystemState.add_package("TestPackage"), this works but leading to memory leakage.
Is there any alternative way to make use of import TestPackage ? How can I use "import <packagename>" in jython without memory leakage?
This is due to a difference in Java and Python.
In Java you can import classes, and static import constants and static methods. You cannot import a package. import java.util; is a compile error, but import java.util.Collections; or import java.util.Collections.*; are fine. A Java package is a way of organizing a bunch of classes but doesn't isn't a class with a constructor, methods, or constants. I like to think of a Java package as a zip file containing all the classes that belong to that package. A Java package is merely a namespace.
In Python you can import modules, classes, and from-import classes, functions, and constants. However, a Python module is a lot more like a Java class than a Java package. It is not merely a "zip file of classes", but a class that contains subclasses. A Python module is a collection of scripts in a folder with an init.py, which defines classes and methods for that module. Thus, you can do things like import collections. Python also lets you do import collections.OrderedDict, from collections import OrderedDict, and from collections import OrderedDict as OD.
I hope this explains why you can't import your Java package. You have to import something (probably a class) from that Java package in order for python to create a variable.
Related
If, in import java.* the * includes all the packages, then why do have to write import java.lang.* and import java.util.*?
import packagename.*;
imports all classes, interfaces ect. from the package packagename but not "subpackages", i.e. import java.*; imports all classes from the java package, but no classes from java.util. Since there are no classes in the java package, using import java.* imports nothing.
Even though typically packages map to file system directories, there is not a hierarchical relationship between packages. A package com.initech is not the parent of com.initech.tps, they are two separate packages with no relationship between them. You can't refer to multiple subpackages with a wildcard because as far as the compiler is concerned there is no such thing as a "subpackage". java.lang is a package, java.util is a package, but java is not a package.
Let's take an example
Suppose you have one package called base.. And inside it you have package called child1..
Base package also have two classes in it..b1 and b2
And your child1 package also have some classes into it..c1,c2..
Then when you write
import base.*
it only imports classes of base package i.e b1 and b2 not the child package classes...
And when you write
import base.child1.*
It imports all classes in child1 package....
In my Xpage, I have added to Java classes. One is "AUser" declared in Models package and one is "AUserRepository" declared in a Repository package.
When I try and import AUser into a class in my AUserRepository... the import statement I type in is not working.
import com.Discussion.utils.AUser;
Weird thing is, If I exit out of Notes and load up eclipse standalone, and make the same kind of stuff, the import works fine. Am I missing out some important factor in Xpages?
The problem definitely is the import routing.
import Models.AUser;
import Repository.AUserRepository;
Is the project build path correct? Project - Properties - Java build path. Also, have you tried cleaning the project? Project - Clean....
I'm developing a project in Java using Vim, and I need to use some classes from the javax package. What should I install and configure to be able to import any of them?
I already installed the JDK 7 and also put it as my default using update-alternative command.
For example, when I compile a class (for example one called GUI) that uses the javax package, the compiler displays:
> javac GUI.java
GUI.java:1: error: package javax does not exists
import javax.swing;
^
1 error
I think it is related to the $CLASSPATH environment variable, but I'm not sure how to tell the compiler where the package is (since it is inside the JDK 7).
Instead of trying to import javax.swing, which does not actually specify anything specific, you should import javax.swing.*. This imports everything in the javax.swing package.
import javax.swing.*;
You can also import specific swing components and utilities:
import javax.swing.JButton;
See The Java Tutorials page on Using Package Members for more information about importing package members.
You should be importing as:
import javax.swing.*;
or else for example
import javax.swing.JFrame;
I've been struggling with my first regex. During the compile, Pattern and Matcher kept getting cannot find symbol errors.
I just changed import java.util.* to import java.util.regex.* and it works like a dream.
I was under the impression that importing java.util.* would also bring in java.util.*.* etc. Is that not the case? I can't find any documentation that addresses this specific question....
No, package imports only get the direct classes in that package (java.* will not import everything, only ones such as Java.SomeClass, not java.util.SomeClass)
Importing java.util.* will not import java.util.*.*.
Yes, that is how package imports work (and are supposed to work) in Java. For example, doing import javax.swing.*; will import all classes within javax.swing.* but not sub-packages and their classes.
Ergo, javax.swing.* will not import javax.swing.event or javax.swing.event.*
Read the following blog for some friendly newbie advice.
See a link and a quoted excerpt from the link below.
http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html
Importing java.awt.* imports all of the types in the java.awt package,
but it does not import java.awt.color, java.awt.font, or any other
java.awt.xxxx packages. If you plan to use the classes and other types
in java.awt.color as well as those in java.awt, you must import both
packages with all their files:
import java.awt.*;
import java.awt.color.*;
I Already read this, this and this before posting my question.
My problem is slightly different.
I'm a beginner in java and I have a jar file that I need to use in my project (I beleive it is called library).
I imported it to my netbean libraries folder using import jar/folder option and I can see content of the library. Unfortunately the class that I need to import is under default package and using import rearrange; (rearrange is name of class) is not working.
To understand how to use this class I decompiled a class file that is importing exact same class and surprisingly it uses import rearrange; to import that class.
I know that I cannot import classes from default package. and my sample class is not using reflection either.
What I'm doing wrong and how can I import this class?
I also know that library (jar file) and also my sample class are compiled with java 1.6 (they didn't use 1.3.x that was allowing this type of import)