How to import a jar file which is in a package? - java

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

Related

Packages in java not compile

Good morning everyone!
I did a project x but all my files were out of order so I decided to group them by folders.
foldera
---ClassA.java
---ClassB.java
folderb
---Class1.java
Main.java
The problem arises when I try to compile, since in the main it appears that the classes I made are not found
I thought this could be solved by putting in the classes
package src.foldera.ClassA;
And in the others the same
package src.foldera.ClassB;
And
package src.folderb.Class1;
So in all classes
And in the main put
import src.foldera.*;
import src.folderb.*;
But I keep getting the same error even though I put the packages
It should be noted that I did not create the folders in the code editor, rather I did it in the same Windows 10 File system
What is this about? Thanks!
Assuming a standard setup where the src folder is the root of your source hierarchy, the statements should be:
package foldera;
package folderb;
The imports should be similarly shortened to:
import foldera.*;
import folderb.*;
You have to define the package in which the class is in.
As an example in classA:
package src.foldera;
And in class1:
package src.folderb;
import src.foldera.ClassA;

How to create a Java package from downloaded source in Linux

I downloaded an external library, common-codecs, and am trying to create a package from the downloaded source code so that i can import and use it in java class files. How would i go about doing this?
I moved the downloaded directory into the same directory as my java class files.
What I've tried so far:
package commons-codec-1.11-src;
I place this at the head of my java class file
Then i try and compile the file using javac in the Linux terminal
javac -cp ~/Documents/javapractice/commons-codec-1.11-src ~/Documents/javapractice/File.java
I get a "interface, class, or enum required error" and the compiler error points to the package statement in the java class file.
code:
import java.util.*
package commons-codec-1.11-src;
public class File
{
........
}
Just to clear things up commons-codec-1.11-src is source code I downloaded and is now a directory in the same directory as File.java
Any help would be greatly appreciated! Thank You!
I downloaded an external library, common-codecs, and am trying to
create a package from the downloaded source code so that i can import
and use it in java class files. How would i go about doing this?
You don't need and you should not package the source code of the external library in your application.
Extracting dependency classes in your own application is a very corner use case and it should done only as you have no choice.
What you need is adding the jar that contains the compiled classes in your classpath at compilation (javac command) and at runtime (java command).
Supposing that the jar is named commons-codec-1.11.jar, to compile your File.java class you should execute :
javac -cp ~/Documents/javapractice/commons-codec-1.11.jar /~/Documents/javapractice/File.java
The File.java declaration is not correct either.
The package declaration has to happen before the import declaration and the package and import values are not correct either.
It should be something as :
package javapractice;
import java.util.*;
public class File {
........
}
About import from the third party library, you need to import classes you use in File class.
You cannot import the whole package as you try to do.
I think that you should try to understand javac/java bases and start with an IDE to make things easier.

Java JAR application: how does the structure look like? (Eclipse)

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.

Java using classes from jar

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.

How to import in Java

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.

Categories