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.
Related
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
In the 'lib' directory of my Eclipse project, I have a jar file, 'foo.jar' which contains a class file 'Foo.class' in the (default package) of 'foo.jar', which I have added to my build path in Eclipse (using Project->Properties->Java Build Path->Libraries->Add JARs...). Now, 'foo.jar' appears under the 'Referenced Libraries' section in Eclipse's Package Explorer.
In the 'src' directory of my project I've got a file 'bar.java' whose first line is:
import foo.Foo;
In the body of 'bar.java', the code can use the contents of 'Foo.class' and all appears well, except I get exactly one error, on the import statement: "The import foo cannot be resolved", so the program won't run; it's the only error in 'bar.java'.
What's the proper way to take care of this?
(I have cleaned the project and refreshed it.)
import xxx.Foo; where xxx is the package name, not the name of the jar
If it's the default package, just try using Foo? You won't need the import in that case.
Thanks m8 ;)
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.
I developed two .jar for my application (LOG.jar and STRING.jar).
I use these jar, with import in .java :
import LOG.CLog
import STRING.CString
It's OK. But .jar is increasing in my project, so I would like to create only one .jar which includes all .jar developed.
So I tried this by creating the only one .jar (named TOOLS.jar) :
jar.exe cvmf MANIFEST.MF TOOLS.jar TOOLS\LOG.jar TOOLS\STRING.jar
But, if I put only TOOLS.jar file in my application compilation (Java build path in Eclipse), I get error when I want to import :
import TOOLS.LOG.CLog
This import cannot be resolved.
And In Eclipse "referenced libraries", I see package PXTOOLS which includes both STRING.jar and LOG.jar, but I don't see STRING and LOG package !
How can I fix it ?
I use these jar, with import in .java
:
import LOG.CLog import STRING.CString
You import classes by qualifying them with package names; not directly from JAR files. I hope your package is called LOG and class is CLog here (though it's a bad naming convention to have uppercase package names)
Secondly, merging JAR files into one isn't recommended. It's best to keep them separate. If at all you did want to merge, you must ensure that you extract all the class files first and then merge.
You cannot include jars in a jar file. If you want to deliver just one jar, check out tools like One-Jar. They can package a working jar for you.
I use these jar, with import in .java :
import LOG.CLog import STRING.CString
You import classes by qualifying them with package names; not directly from JAR files. I hope your package is called LOG and class is CLog here (though it's a bad naming convention to have uppercase package names)
Secondly, merging JAR files into one isn't recommended. It's best to keep them separate. If at all you did want to merge, you must ensure that you extract all the class files first and then merge.
Yes, you can do so. Have a look here - Build Java entire project jar using JDeveloper
I am trying to import some existing projects into Eclipse.
The structures for their packages is:
Project/
/src
/java
/a
/b
/c
Once imported in the package explorer I see:
Project
src/java
--a
--b
--c
- AClass.java
This is ok, since the classes e.g. AClass.java are defined in package: a.b.c
But in one project the structure (once imported) becomes:
Project
src
--java
--a
--b
--c
- AClass.java
And that causes the error that AClass.java is defined to be in package a.b.c but it is actually under java.a.b.c
Why is this happening? Why in this specific project java is not ignored as part of package?
Thanks
How are you creating the Eclipse projects? It sounds like you just need to put "java" as a root on on the source path here, instead of "src". You can do this by editing the build path after the import process, of course.
Remove the existing source folders first. -right click -> menu -> build path -> remove from build path
then
Right click on the source folder. build path -> use as source folder.
Seems like your settings are pointing to the parent of the source folder so src is recognized as package by eclipse.
Wrong package name when using automatically added imports in Eclipse
call the package on the top of your import statements,
like if your class is in java/main/org/goal/Main.java
then the path is package java.main.org.goal;
else do Ctrl +1 and it suggest some quick help
import the necessary package from that
Use this sentence import java.io.*; at the top of java file. Otherwise, you have to create package folder.
Import statements:
In Java if a fully qualified name, which includes the package and the class name, is given then the compiler can easily locate the source code or classes. Import statement is a way of giving the proper location for the compiler to find that particular class.
For example, the following line would ask compiler to load all the classes available in directory java_installation/java/io :
import java.io.*;