How can I install additional packages in Java? - java

I am very new to Java.
I would like to use some features from package called daj
The tutorial code has the following lines.
import daj.*;
import java.util.*;
import java.lang.Math;
import Msg;
But first and fourth lines produce red underscore such that program cannot compiles.
How can I resolve this problem?

You have to add the packages to the classpath. If you are using an IDE as Eclipse or Netbeans, go to the project options and find the "Add library/jar" and they will take care of you.
You will need to add these libraries to the classpath both to compile and to run your program.
Also, the usual way of ordering imports is:
first, java. packages
second, javax. packagaes
third party packages
your packages

You need to have them in your build path.

That daj package or jar file should be in your package or current directory or in classpath.

Related

How to import correct package for StdDraw?

I want to use the StdDraw package, and I've tried many different ways of importing it.
Of course I tried:
import StdDraw;
But, when I look at the documentation from Princeton here, it shows that StdDraw is part of Objects, so I try this:
import java.lang.Object.StdDraw;
However, this results in an error:
error: cannot find symbol in
import java.lang.Object.StdDraw;
I saw this question here but it does not answer this question either.
How do I import StdDraw? thank you.
if you want to use StdDraw you must have
either the sources
or the classes (best zipped up as jar)
as preferred way you use the sources (see http://introcs.cs.princeton.edu/java/15inout/). it says there "To use these libraries, download StdIn.java, StdOut.java, StdDraw.java, and StdAudio.java into the same directory as your program. "
once you did this the imports should be working.
NOTE: all four files are not in packages, so you should 'download' them into the 'standard' package. That means you have to download them to the root package of your project.
by the way: don't import import java.lang.Object.StdDraw; but do just import import StdDraw;
First of all check encoding of your IDE. It should be set to UTF-8. It is important if you are using MS Windows operating system.
Then create StdDraw.java class in the same package as the package of your program you are writing. Remove class declaration, leave only package declaration.
Then visit this page: https://introcs.cs.princeton.edu/java/stdlib/StdDraw.java .
Copy all its contents (Ctr-A, Ctrl-C) and then paste it into StdDraw.java file you created previously.
StdDraw.java has its own main method so try to run it in order to check that the library works correctly. You should see a window with four strange figures :) .
Don't touch StdDraw.java anymore. Now you can easily import StdDraw library and refer to its methods with name of the class.
Enjoy

Why don't I need to use the import keyword when importing a library?

I imported an external JAR to my project in Eclipse, by following these instructions:
Right click on the project > Build Path > Add external archives > Choosing the JAR file from the hard drive.
The JAR file than appears in the 'References libraries' section in my project, and works fine.
However, I never need to use the import keyword in my classes in order to use the classes from the JAR. I find this weird, I thought I'd have to use import myImportedJar or something similar for this to work.
Is it normal that I don't have to use the import keyword? Did I do something wrong?
The import statement is used to be able to refer to types and their members by their simple names. You don't need to import classes that are in the same package, unless they are nested members of other classes. This is true regardless of where the class comes from, for example, if it's in another .jar.
The import keyword works on a package level. If these packages are supplied by jars or not is not a feature of the java programming language.
import only makes a name available in unqualified form in your program. The following code fragments are identical:
java.util.List<Object> list = new java.util.ArrayList<>()
vs
import java.util.List;
import java.util.ArrayList;
List<Object> list = new ArrayList<>()
Now, importing a jar file in eclipse puts the classes from this jarfile on the classpath - the total "world" of available classes for your application.

Creating a Java Library or class or package with Jar files

I have about 100 jar files and I think I want to make a library with them. What is the best way to do this? How does importing work with this. Do I still have to ask for each class or do I just refer to the new library?
More detail
I have the GeoTools9.4 package (in a zip). It has about 100 jar files. When I import these into my project in eclipse, it takes each jar file and stuffs it in and clutters up my structure. So I think I want a library (or a package or a class) I am not sure what the terminology is here.
More detail on how to call the classes in the new library.
Right now here is how I call the classes
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
If I put all of these jar files in a library can I replace the above lines with a new import like
import org.geotools.local
or do I not need to change the way they are called?
I propose you to use a Maven for this stuff.
Maven is a greay build tool, that could take care of problems, like adding dependency jars to a project. Also, GeoTools support Maven and have a nice manual for it (http://docs.geotools.org/latest/userguide/tutorial/quickstart/maven.html)
About last question - when you'll add this libraries, full name of these classes will be the same, so you must import and use them as you import them right now.
This is not usually refered to as "calling" the classes, but rather importing the classes meaning that they become available to the class that uses them.
No matter how you have those classes (in many jars or a single jar) you still need to have the import statements in the beggining of the class file for the source to be compiled
I am not sure how Eclipse "clutters up" your structure. You can place all the jars in a single folder e.g. lib and then import them into your eclipse project from that folder. If you mean that the jars show up in the left pane then there are filters that can hide them. In Eclipse there is the concept of a Working set where you can select what it would be visible and what not.
I hope it helps

Import custom java file

I'm currently trying to read some source code in Java I found online to study and learn the material. I want to compile the files first to make sure they work before I study it. When I try to compile though, the compiler complains that it can't find some of the files it needs to import. So opening up the main.java, I find
package br.com.seimos.minijava;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import br.com.seimos.minijava.parser.MiniJavaParser;
import br.com.seimos.minijava.syntaxtree.Program;
import br.com.seimos.minijava.visitor.TreeDumper;
import br.com.seimos.minijava.visitor.TreeFormatter;
The errors I'm getting are coming from not being able to find MiniJavaParser, TreeDumper...the 3rd chunk of code. Those files exist in the same directory as the main file though, so what is going on? What is br.com.seimos.minijava stuff? I tried putting the files in that those folders (as in br\com\seimos\minijava\PUT_FILES_HERE) but still no good. Does br need to be in the root directory?
Thanks, I realize this is probably a really elementary question...
The required directory structure is br/com/seimos/minijava/OTHER_FOLDERS/SOURCE_FILES.java. For instance, the path to MiniJavaParser should be br/com/seimos/minijava/parser/MiniJavaParser.java. You should then run the Java compiler from the parent directory of br.
If you're using Eclipse or another IDE, you should configure your project settings to handle this.
For import br.com.seimos.minijava.parser.MiniJavaParser; your MiniJavaParser class must be in the directory br\com\seimos\minijava\parser\ and not br\com\seimos\minijava\. Similarly for other classes. Try changing it.
You're on the right track. You will need to put those files in br/com/seimos/minijava/... as indicated by the package name of each. Java requires that you put files in a directory hierarchy that matches their package names.
Then, you'll want to compile using a command like:
javac br/com/seimos/minijava/parser/MiniJavaParser.java
This is all a bit inconvenient from the command line, especially for a larger project, so you might be better of getting a Java IDE and having it help you arrange the files.

The import javazoom cannot be resolved

hi all
i use the javazoom.jl.player.Player package but it is says The import javazoom cannot be resolved. i am using eclipse and doing Android project. i clean the project still the same error is shown. please correct me.
If eclipse can't resolve a package fragment of an import statement, then it tells you (with that error), that there is no library on the classpath that contains a class from that package (or from a package whose name starts with the missing part).
An easy way for standard java/eclipse:
create a folder lib in your projects root directory (with the eclipse workbench!)
copy and paste the jar into that folder
right-click the copied jar and select "add to build path".
This should eliminate the compiler errors immediately.
(Previous part of the answer)
Taking the error message literally, it looks like you have a line of code like that:
import javazoom;
This would be wrong, because we don't import packages but classes from a package. To import all classes from the javazoom package, we'd say:
import javazoom.*;
You should download the .jar of jLayer ( http://www.javazoom.net/javalayer/sources.html )
And add into classpath in the way Andreas_D told you.

Categories