how to import javax in java? - java

I need an object of javax.servlet.jsp.JspWriter in my JAVA program. I have to import javax, but I do not know how to do it.
I use ant for compiling my project and no IDE, just commandline.
Should I download a package or something and put it in my project? Or there is another way?

An IDE would ease thing considerable. Ant intergrates with Eclipse and helps to ease management of imports.
For the commandline; download the appropriate jar, add it to the classpath using -Djava.ext.dirs=
I presume that you can take it from here!

To import javax.servlet.jsp.JspWriter you place an import statement at the top of your file, after the package declaration, but before the class declaration. The import statement would look like:
import javax.servlet.jsp.JspWriter;
This tells the compiler to interpret uses of JspWriter as javax.servlet.jsp.JspWriter
For example:
package somepackage;
import javax.servlet.jsp.JspWriter;
public class AClass {
//Class contents here
}

I need an object of javax.servlet.jsp.JspWriter in my JAVA program. I
have to import javax
No you don't, you have to import javax.servlet.jsp.JspWriter. So do that. If it doesn't compile, adjust your classpath or your IDE project settings to include the appropriate JAR file.

Related

Why is the Eclipse IDE giving me "editor does not contain a main type" even though I have a main?

I'm new to Java and in an attempt to make library installation easier, I've decided to use Eclipse IDE. However, my code is not compiling because it says "launch error: Editor does not contain a main type"
Here you can see what the layout of my project folder looks like
It's a beginner's project, so there is a folder called src and inside there is another called ww. Inside this one, there are two files, connectToData.java and Main.java. The connect file contains a structure like this:
package ww;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import org.json.JSONObject;
public class connectToData {
public connectToData() {
throw new IllegalArgumentException();
}
public static JSONObject getData(String query) {
String u = "https://sky.esa.int/esasky-tap/tap/sync?request=doQuery&lang=ADQL&format=json&query=";
query = "SELECT+TOP+10+,*+FROM+observations.mv_v_v_hst_mmi_observation_optical_fdw_fdw";
URL url;
And my Main.java has a structure like this:
package ww;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
System.out.println("Hello, please fix the problems with this code");
They both don't have any other classes or functions inside. Why am I not able to run my code, even though I clearly have a public static void main?
That outlined 'J' means:
This is a java file, but, is not part of any source folder.
Which means you can't run these.
Projects are complex beasts. You don't just have source files - you have a so-called 'source folder'. For example, you could have an application that has test code, a separate installer project, and a plugin for photoshop, in addition to a main app. Obviously, you would want to separate these things out, so that the build tools can for example not include the test code in the plugin, that'd be weird.
As a consequence, projects have so-called 'source folders'. You need at least one, and you can have more than one if you want. In fact, the 'standard model' of what maven projects look like specifically requires that your main app is in src/main/java, for example.
Given that you must have source folders, that also means you can put java files outside of a source folder. And when you do that, you get that J-outline icon as your screenshot shows. As far as eclipse is concerned, these files aren't runnable at all. Perhaps they exist as a template for something (no different from an icon file), or as part of documentation.
The solution is to put these files inside a source folder. The information you provided doesn't let me give you a specific series of steps, but, generally, rightclick on your 'ww' project, find the build settings, add a source folder. Then drag the files you have into the source folder.

Can't Import packaged class files from a jar in jython when it contains classes in the default package

I am working on a program that needs to import a jar that has classes both in the default package (root of the jar) and in packages.
So far I have this code and it works so i can import the ones in default but it fails when trying to import ones on packages.
import sys
import os
sys.path.append(os.getcwd() + "/versions/1.7.2.jar")
If I run import a (a is a class as this is a Obfuscated jar.)
it imports but if I run import net.minecraft.server.MinecraftServer it does not work it says No Module named net.
Which I know the class and all packages around it is there so any help?
The problem is that Jython does not correctly find the code.
The source code is based on files like a.class wich you can import but not net.minecraft.server.MinecraftServer.

Java library import difficulty

I'm trying to compile a small test program I have written for a raspberry pi.
The program makes use of the Pi4J library to control the piface add-on board.
What I have done so far is based on the following tutorial: http://www.savagehomeautomation.com/piface
While I can get the above example program to compile within my IDE after setting up the class paths I get compile errors with the one I have made and as far as I can tell the imports are set up in the same way.
It says that each of the following packages does not exist:
import java.io.IOException;
import com.pi4j.component.switches.SwitchListener;
import com.pi4j.component.switches.SwitchState;
import com.pi4j.component.switches.SwitchStateChangeEvent;
import com.pi4j.device.piface.PiFace;
import com.pi4j.device.piface.PiFaceLed;
import com.pi4j.device.piface.PiFaceRelay;
import com.pi4j.device.piface.PiFaceSwitch;
import com.pi4j.device.piface.impl.PiFaceDevice;
import com.pi4j.wiringpi.Spi;
I'm assuming that my problem is relatively simple but I don't really understand how import statements work and it's quite vague topic to search about. I have included some file paths if that helps.
This is where my project resides:
/home/pi/JBerries/relay
and this is where the pi4j library is:
/opt/pi4j
I hope the following image provides some of the information requested, note that the class paths are already set up:
You need to set the CLASSPATH environment variable to /opt/pi4j or the jar file therein. WIthout this the compiler is unable to know where your libary is located and will give you the errors you describe.
If you're using a project in JBerries you need to configure the classpath for the project - the screenshot shows the classpath for single-file compilations only. To edit the project config right-click the root node in the project window and select properties.

Java library class not recognized?

I am importing this following :
import org.apache.lucene.analysis.PorterStemmer
in Java program. The whole package is available in refrenced library.
I tried importing
import org.apache.lucene.analysis.PorterStemFilter
and
import org.apache.lucene.analysis.Analyzer;
both are working fine except the first one mentioned
Can anybody point out why ?!
Package org.apache.lucene.analysis.PorterStemmer is not a public package which is why you cannot import it. If you look at this package inside the library, you'll notice that it begins with class PorterStemmer instead of public class PorterStemmer.
My guess is that you have a different version of the Lucene JAR that doesn't contain the class that's failing to work. Open the JAR with WinZip, 7Zip, or some other tool and see if that class is indeed missing. If it is, you either need to find a version of the JAR that has it or rewrite your code to use an alternative.

Com.Google.Common.*.*... import failure

I'm working on a project and need to use the Predicate interface of google's common.base
I tried importing the google-collect jar at http://code.google.com/p/gdata-java-client/source/browse/#svn/trunk/java/deps
I got the jar, imported it in my library but nothing's there...
Can anyone tell help me to be able to work with this interface?
If you are using eclipse, you need to add the library jar to your build path, as described here.
If you are compiling from the command line, use the -classpath option to add the jar to the path.
Also make sure the case is correct in your import declaration. The import should look like
import com.google.common.base.Predicate;
not
import Com.Google.Common.Base.Predicate;
Also you probably know this already, but the import com.google.common.*.* suggested by your question title is invalid - you can only have one *. (import com.google.common.base.* is OK.)

Categories

Resources