Java using classes from jar - java

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.

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;

Java compiler doesn't see sibling packages

From Windows command line, I am trying to compile a java file in package com.example.web that imports a class from package com.example.model but keep getting the message that the package com.example.model does not exist.
From what I can tell, the compiler doesn't recognize sibling packages for some reason, as I do not get the error message when I try to import the package com.example but I do get it if I try to import com.example.test. I've compiled the files in the other packages that are not dependent on imports without an issue from the same directory. I'm almost positive that I've had access to sibling packages from the command line in the past.
package com.example.web;
import com.example.*;
import com.example.test.*;
import com.example.model.*;
public class Test{
}
I get 2 errors saying that package com.example.model and com.example.test do not exist. They're in my file structure, they have .class files in them.
UPDATE: It works if I add com.example.model and com.example.test to my classpath, but I was under the impression this was unnecessary if the dependencies shared the same parent folder as the class I was compiling. Am I mistaken? Did this use to be the case but changed in the last few years? It's been awhile since I've compiled directly from command line, but I don't remember having to do this before.

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.

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

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

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