I'm trying to import, and my statement looks like this:
import C.Users.pro-services.Documents.JAVA.libs.feed.synd.SyndFeed;
The compiler throws an error b/c of the hyphen in pro-services. Is there an easy fix, or a deeper issue here(re: my understanding of import statements!)?
Thanks
the package pro-services is wrong. You can't create a package with hyphen.
You need the SyndFeed on the classpath, and refer to it by its fully-qualified name.
Roughly, the classpath contains of multiple jar files and individual class files. Each class (within or outside a .jar) has a fully-qualified name: the package name + the class name. That's what you write in your import statement.
Oh my God, please tell me you're not referring to file's absolute path!
In Java, there is a thing called classpath which the Virtual Machine uses to locate classes.
Then, once your class is in the classpath (either in a jar file or inside your application), you can import it to your classes and use it.
The class you're trying to import seems to be com.sun.syndication.feed.synd.SyndFeed
which you would import like:
import com.sun.syndication.feed.synd.SyndFeed;
That's considering you have it in your class-path. IF you don't, get the jar and put it in the classpath.
The argument to import is not a path; it's the name of a Java package. At the top of SyndFeed.java, it should say something like
package feed.synd;
the argument to package should be the prefix for the class name in the import; i.e.,
import feed.synd.SyndFeed;
You don't use import to tell the compiler where to find the class; the -classpath switch (or, less nicely, the CLASSPATH environment variable) are used for that.
It looks like the package for the SyndFeed class is feed.synd. If this is the case, do the following:
import feed.synd.SyndFeed; in the java file.
If the SyndFeed class is in a jar, add the jar to the classpath. Perhaps CLASSPATH = C:\Users\pro-services\Documents\JAVA\libs\SyndFeedJarName.jar.
If the SyndFeed class is not in a jar, add the directory that corresponds to the first name in the package (in this case, feed) to the classpath. Perhaps CLASSPATH = C:\Users\pro-services\Documents\JAVA\libs\feed.
Related
I'm trying to work off of an existing code base, and one of the files has an import statement accessing the path game.format.noFlash.*. However the format folder only has files for the other classes; the noFlash file folder is nowhere to be found.
I know it must be there because when I begin to type in the import statement in intelliJ, one of the autofill options is the noFlash folder. Is there a way to access it, and if so how?
The auto-fill options are display packages and also class names
I suspect the noFlash is a class and not a folder and Intellij is suggestion that you can import it, for example:
Classes can be specified explicitly on import instead of using the wildcard character.
import javax.swing.JOptionPane; // Make a single class visible.
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.
I have a jar file named stdlib-package, the .class files in this jar file are all in a edu.princeton.cs.introcs package
For example, I want to use one class named StdDraw from that jar file in my own codes
Say E:\code is my current working directory. like other conditions, I create a subdirectory bin\edu\princeton\cs\introcs to put stdlib-package in
Here is my codes:
package com.david.test;
import edu.princeton.cs.introcs.stdlib-package.*; // any problems here ?
public class DrawTest
{
public static void main(String[] args)
{
StdDraw.line(0.5, 0.5, 1, 0.5);
}
}
Then I put DrawTest.java in the src\com\david\test
To compile the DrawTest.java, I type the commands in command line:
javac -d bin -cp bin\edu\princeton\cs\introcs\stdlib-package.jar src\com\david\test\DrawTest.java
But it failed : package edu.princeton.cs.introcs.stdlib_package doesn't exist...
I searched for a lot of time, but haven't found the answer
Thanks for your help
EDIT===
#EJP's comment solves my question too.
This should be all you need in code:
import edu.princeton.cs.introcs.*
Then you only need to make sure the jar is on your class path; it doesn't matter what the name of the jar is, or where it is. If it is on your classpath, Java will load it, and use the location of the files within it.
Move the jar into a lib directory in your current directory, and do something like
javac -cp lib\stdlib-package.jar src\com\david\test\DrawTest.javac
Your import is wrong. There's no such package in that .jar file.
import edu.princeton.cs.introcs.*;
In general, however, that's a bad practice and you should really only import what you need:
import edu.princeton.cs.introcs.StdDraw;
I understand that there is no package with 'stdlib-package' name.
If the fully qualified name of the class you are using is edu.princeton.cs.introcs.StdDraw, you can just import 'edu.princeton.cs.introcs.StdDraw'
import edu.princeton.cs.introcs.StdDraw
If you want to load all the classes in that package
import edu.princeton.cs.introcs.*
I'm a seasoned PHP programmer with backgrounds within Powershell, JavaScript etc.
I see the benefits with Java and wish to dig deeper.
All the documentation though is too advanced for me atm.
A typical PHP structure:
index.php
classes/class_get.php
classes/class_set.php
Where in index.php you would have the lines
require_once "classes/class_get.php";
require_once "classes/class_set.php";
Now in Java...
I understand Windows files are in .JAR format. No problem here, easy to understand.
I am confused when creating this in eclipse. Is the strucutre;
Main.java
src*/defaultPackage**/myClass.java
* folder
** package
Where in the Main.java file you would declare "import myClass.java"?
Also other packages such as the following:
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
I am getting errors, and this manifest file, I haven't heard of it in any tutorials yet?
Try this, this is the way to create a jar or runnable jar in eclipse
File -> Export-> Java ->Runnbale JAR file
Launch configuration : your Class containing the public static void main(String[] args)
Export destination : Target place
Library Handling:
Package required libraries into generated JAR
FINISH
a) There are no Windows files in Java. Java is cross platform.
b) Something with a slash delimiter is always a folder. Something with a dot is always a package. Don't confuse them, because it is confusing enough.
c) Don't use the term "defaulPackage", because there is such a term for the case, that you don't specify any package. Then the package of your class is called the default package.
Main.java
src*/defaultPackage**/myClass.java
Where in the Main.java file you would declare "import myClass.java"?
You never import something .java, because you import a class, not a source file. Often you only have third party compiled classes in a jar, and don't have the source. Well - maybe.
If your class belongs to a package, the name of the class is the whole package name. You can omit it from classes in the same package.
So we don't know whether Main and MyClass (use Upper case, if you like to communicate with others - else you're confusing us) belong to the same package.
If so: Don't import anything.
Else: Import the whole package name, which might contain multiple dots.
So for example:
import yourCompany.games.monstersahead.*;
or
import yourCompany.games.monstersahead.MyClass;
for example.
The package name will usually not contain folder names like src, bin, classes.
This must be a super overasked question. Although here goes:
I have a java file for testing around (hworld.java) and am trying to import conio.jar, a JAR which is a wrapper of Conio. The JAR contains only one class file (conio.class) and META-INF. Trying to do import conio.* or import conio.conio shows me this:
C:\Documents and Settings\Nick\Desktop>javac -cp *.jar; hworld.java
hworld.java:3: error: package conio does not exist
import conio.*;
^
1 error
And compiling it like javac -cp conio.jar hworld.java still errors out while compiling. I even extracted the jar and had conio.class in the same directory as hworld.java but to no avail. The JAR is in the same directory as hworld.java, as well.
Anyone have any idea on how to fix this?
You don't mention whether conio.class is defined in package conio. If it is not, then simply use the class without importing it. Remove the import.
It's actually not possible. You need to put the other class in a package if you want to import it.
What's the syntax to import a class in a default package in Java?
Find out what package Conio is in - an easy way to do this is to open the jar as a zip file, the package will correspond with the folder structure of the archive. For example if Conio is in x/y/z then import x.y.z.Conio and compile/run with conio.jar on the classmate.