Importing javax package in Java without an IDE - java

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;

Related

Eclipse cannot import import com.google.maps.* (For java Fx programme)

I am working on a Java fx application and I need to import some libraries from com.google.maps.
I imported these libraries :
import com.google.maps.GeoApiContext;
import com.google.maps.PlaceDetailsRequest;
import com.google.maps.PlacesApi;
import com.google.maps.QueryAutocompleteRequest;
import com.google.maps.errors.ApiException;
import com.google.maps.model.AddressComponent;
import com.google.maps.model.AddressComponentType;
import com.google.maps.model.AutocompletePrediction;
import com.google.maps.model.PlaceDetails;
The java import statement is a little bit misnamed, it really means alias. import com.google.maps.GeoApiContext; really just means: Any time you find the type GeoApiContext anywhere in this source file, assume I meant to write com.google.maps.GeoApiContext.
Crucially, it does not 'invoke' any code in that class whatsoever, nor does it find or download any dependencies from the internet for you.
You will need to find the jar(s) that provide these classes and put them on the classpath of this project.
It can be as simple as downloading the relevant jar (perhaps com.google.maps-google-maps-services.jar?), put it in a lib dir someplace inside this project, finding that in the package explorer, right clicking it, and selecting 'add to classpath'.
Or, more likely, you want to use gradle or maven to take care of this for you: These tools turn simply mentioning the dependency in a list of libraries you require into automatically finding that on the internet, downloading it, configuring your IDE so that it knows where it is, and using that dependency during build and run steps.

Jython Java Package Import Statement

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.

Why I cannot use Arrays class in my Java project?

I am trying to compile this program in my IntelliJ IDE, but my IDE returns that it cannot resolve Arrays symbol.
Click to enlarge
Note that as you see in the left sidebar,I already added all the required libraries to my project!
This is imports of my program :
import java.io.*;
import java.util.*;
import java.security.*;
import java.security.Security;
import java.security.cert.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.cms.*;
import java.util.Arrays;
As you see I added import java.util.Arrays to the program imports also, but nothing changed and I still have the above problem with Arrays.
Note that I tried to run the program on another computer, with the same version of IntelliJ IDE, but JDK 1.7 installed, and I have this problem there, but In my current computer (That JDK 1.8 is installed), the above problem appears.
Update:
In the line import java.util.Arrays, Arrays is red and that means there is no Arrays class in util. Why I should have this error, while I install JDK 1.8 and added all of its libraries to my project?

How to import my java class into another java class in Xpages java

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....

Import a class from default package

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)

Categories