How to use classes from the .jar lib added to netbeans project? - java

Can anybody help me out in this?
what I have is a abc.jar file with me. It contains ABC.class file inside it. I added the jar file to netbeans project Libraries. But I am getting an error when I write
ABC a=new ABC();
Error :
"can not find symbol class ABC"
any help?
Edited :
also I am able to see the structure of ABC class when I click on the ABC.class file inside the library.

Remember to import it with an import statement. You should probably read the documentation that follows the jar file. Hopefully there's an example of usage - mostly there is.

The answer :
I found out that if the jar has only default package you won't be able to import classes inside it. You need to have packages other than default inside the jar to import it. You can only import non default packages.
the thing with default packages works fins in eclipse, but not in netbeans.

Related

The declared package "facts" does not match the expected package ""

I am using Eclipse IDE for Enterprise java and web developers. I downloaded some zip file called facts(1) and followed every step in setting up this facts project. When I try to run it on server it showing " ... doesn't match the expected package. I tried by adding import package facts; and also without that! I have also looked similar answer and didn't able to fix that either. FYI i have jdk (16.0.2) currently.
[enter image descripenter image description heretion here]2
enter image description here
As you are using default package for your facts classes like Fact.java, FactList.java, you do not need to import it.
However, I think your fact files Fact.java, FactList.java, etc are also failing compilation as the package declaration on those files are not same as the folder structure.
So please try these:
Make sure that there is no statement
package xxx.yyy.zzz;
in your fact classes Fact.java, FactList.java, etc and they are compiling successfully i.e. no errors in those files.
Remove the import statement in FactServlet.java as it is not required.
But.. according to Java standards using default package is not advisable. Create proper directory structure for your files.
you classes Fact.java, FactList.java, FactSearchModel.java and few more are in default package
create a package and move these classes to package, then import that package wherever required

Error importing package in Java

I am trying to sort my classes into packages but i can't import them.
My files are in the following folders:
- .java files are in C:\Java\Code\src\my\app\Timer
- .class files are in C:\Java\Code\compiled\my\app\Timer
In my class (timer) i've added package my.app;
Also, I have setted the CLASSPATH to look in both src and compiled folders.
Then, I have another folder where I put my "bigger" projects in:
- C:\Java\Projects\myProject
The problem is that when I try to import the class Timer into MyProject using import my.app.*; all I get is:
Error: package my.app does not exist
Culd you please give me a hand?
PS. My IDE is Dr.Java
I have found the problem.
It appears that Dr.Java ignores complitely the CLASSPATH variable. It is necessary to set in preferences where are the .class files.

Importing own java files in jsp

I'm kind of a JSP noob (and I'm forced to write old school because of school) and I have this pure java file that I wrote. I want to use that FooBar.java file and the class inside it (packagename.name.FooBar) inside my .jsp files. My questions are:
Where should I place my .java files? Some places told me to put them in /src and some told me to put them in /WebContent/WEB-INF/classes. Both of these don't work.
How can I import them correctly? I'm trying <%# page import = "packagename.name.*" %> and it doesn't work in both cases above (when the package is in src or in classes.
EDIT: Now I've tried compiling them and putting them in WEB-INF/classes/packagename/name, but I still get errors:
Only a type can be imported. packagename.name.FooBar resolves to a package
And then of course these (because it didn't import correctly): FooBar cannot be resolved to a type.
What am I doing wrong?
EDIT: Thank you everyone! If you're wondering, here's what solved my problem:
As #user7294900 mentioned, you can only import .class files and not .java files. Use the javac command to compile files - here's more information.
If you get the resolves to a package error ensure that the files are in the right place, for example if you have C class in package a.b you need the C.class file to be in WEB-INF/classes/a/b/C.class. If it is, try simply restarting your IDE/server.
JSP file can import files with class extension and not java extension.
Class file is a compiled java file.
You need to compile you java file which will create FooBar.class
One option is to save FooBar.class file in a jar under /WEB-INF/lib
Second option is save FooBar.class inside your classes folder under relevant pack for example if your package is a.b /WEB-INF/classes/a/b/FooBar.class

How to call java classes of a compiled jar file within android studio?

Hello StackOverflow folks,
I have a jar file of java classes. I added this jar file to my android studio project under folder /libs. Now, what I want to do is use those classes within the jar file in MainActiviy.java. I just do not know how.
Some details:
My jar file is named: zombi.jar.
The class within the jar file to call is named: COMBI.class
I tried the following:
In MainActivity.java, I wrote:
// declared class variable
private COMBI mCOMBI;
Then, in OnCreate method, I wrote:
mCOMBI = new COMBI();
//to start calling method COMBIStart to launch the command-line system
mCOMBI.COMBIStart();
I actually called the classes as I would in normal Java. I think Android uses special java code that looks like java, but I don't know how to use them.
I could not get the code to work.
Can you help me?
I'm going to assume you've already written your import statement for the jar class. If you already put the jar file in the /lib folder, Android Studio should update your build.gradle file. Check to see if you have a link to the jar file in your dependencies{...}
If not, you can add it manually.
Just like in normal java you have to add an import to the correct package at the top of your file for everything that's not in the same package the file is in.
It should look something like this:
import android.view.View;
but instead of android.view.view it would be a reference to the class in the jar you're trying to add

Java: silly newbie issue about path used in import statement

I'm working through the example here:
http://www.vogella.com/articles/JavaPDF/article.html
In my file, I've got:
package com.mycompanyname.mydirectory;
import com.mycompanyname.OneOfMyClasses;
import com.itextpdf.text.Document;
...
public class MyClass {
...
}
Everything is working fine. What I don't understand is that since I just copied the import statement directly from the link above for the iText portion -- why does com.itextpdf.text.Document work?
I mean, if I look in directory com.mycompanyname I can see OneOfMyClasses.java there.
But in the com directly, there is no itextpdf directory (although maybe my user doesn't have permission to see it(?)).
Hoping someone can help me understand what I'm missing here. Doesn't the import point to a specific directory that I should be able to see the class? Is there a different com directory somewhere that iText is using, and com.itextpdf.text points to there? (if so, where's the directory located)?
I installed the jar file for iText in the lib folder as per usual, and made sure it was included in the classpath.
Those classes are inside a JAR file that is added to the classpath:
Create a new Java project "de.vogella.itext.write" with the package "de.vogella.itext.write". Create a folder "lib" and put the iText library (jar file) into this folder. Add the jar to your classpath.
import statements will look inside whatever directory trees are in the classpath, which includes the current directory at compilation time (tipically the src/ directory in your project) as well as any directory specified through environment variable or JVM startup parameter. See this about the classpath.
EDIT
You do need the imports whenever you use classes across packages. Every public class/interface you define is in a package. If whatever you are referencing belongs to another package, you need to import it.
JARs are zip files that contain directories and files inside. It's the same as plain directories and files, only packed.
It comes from the iText dependency (jar) you added in an earlier step.
Not necessarily - you could also import from libraries, etc.
In fact, Java will try to search through the classpath. Here is some helpful documentation.
That class is most probably imported in a JAR library. Inside such JAR file, the class files are kept in exact package/folder structure as you use when importing them.

Categories

Resources