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
Related
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.
What is the difference between commons-io-2.0.jar and commons-io-2.0-sources.jar?
When I import commons-io-2.0-sources.jar in Netbeans the import org.apache.commns.io.FilenameUtils is not working. And when import commons-io-2.0.jar it works fine. Why?
commons-io-2.0.jar
This file contains the classes which you wants to use with your application.
This contains only .class files.
commons-io-2.0-sources.jar
This file contains the actual source for that classes.
This contains .java files
Hope this helps you :)
Reason you get error is, when you have commons-io-2.0-sources.jar in your class path is, this jar contains java source and not the class (compiled java files) files.
While commons-io-2.0.jar does contains already compiled java source files i.e. class files and hence your code compiles.
commons-io-2.0.jar is the actual jar file and commons-io-2.0-sources.jar is the source for that jar. This helps to view the source code for debugging purpose.
Recently started studying Java for an exam.
While learning packages, tried this and got an error message. What I did was
//Creating class A (Within package the package: com.test.helpers)
package com.test.helpers;
public class A {
public void sayHello(){
System.out.println("Hello World");
}
}
//And then the class App utilising the class A
import com.test.helpers.*;
public class App{
public static void main(String args[]){
A a = new A();
a.sayHello();
}
}
I had both of these files in a directory called 'JavaTest' (on Windows 7), and first compiled the A.java using the command javac -d . A.java
And then, while attempting to compile App.java, I got the following error message:
App.java:5: error: cannot access A
A a = new A();
^
bad source file: .\A.java
file does not contain class A
Please remove or make sure it appears in the correct subdirectory of the source path.
1 error
However, the problem seems to resolve in two ways,
Deleting the Source file A.java
Changing the import statement from import com.test.helpers.*; to import com.test.helpers.A in the file App.java.
I'd be highly grateful if you can explain what happens here. Or I might be making a goofy human mistake or a syntax error.
Here's the link to the source files
Hi the problem here is that the JVM confuses the class file due to the ambiguous class file name in both the directory (the JavaTest as well as the com.test.helpers directory).
when you do javac -d . A.java the compiler makes a class file in the directory com.test.helpers and now it confuses it with the sourcefile there in JavaTest
Deleting the Source file A.java
When you delete the source file A.java from JavaTest, JVM knows now that the class file in com.test.... is to be used, ambiguity goes away.
Changing the import statement from 'import com.test.helpers.*;' to 'import com.test.helpers.A' in the file, 'App.java'.
Here you are specifying which particular file to use in your class implementation that is you are telling the compiler to use the file A.java from com.test... and not from JavaTest package
Now, the solution for this ambiguity to not ever be a problem for you, you must import the specific files with import statement i.e. import com.test.helpers.A; or if you want to do import com.test.helpers.*; then you must specifically use com.test.helpers.A in place of A everywhere in your current class implementation to tell the compiler not to confuse it with the source at JavaTest
I know it's a lot late for this particular answer, but I wanted to share my views for the upcoming readers, if it could help them in any way, it would be great.
Thanks!
move the A.java under folder JavaTest to com/test/helpers. the error you are seeing is for the compiler complaining that A.java is in a folder that does not match its package declaration. Remember, you cannot access A from App without A being in a package.
from under src driectory run the following command to compile your classes
src> javac ./*.java ./com/test/helpers/*.java
then from under src folder
src>java App
that should run your program.
the file A.java should be in the path JavaTest\com\test\helpers\A.java
and don't forget to compile it like this:
javac -d . com\test\helpers\A.java
every thing is in right place, java does not ask to put your A.java file, in the helpers folder
the whole reason why your code ran with the A.java file removed and not otherwise is:
when your app.java (or the main java file which is importing other classes in it, -->) is in a default package then while importing the classes it gives priority to the same directory and not to the package directory(if there exist any file that has the same name as the class file -> and thats why it gives error bad source file A.java -> as it wanted A.class)
And because of that there is a rule in java : That never place a .java file (of the same name)in parallel to the packages
so to solve this problem you have to either remove the file, A.java
or rename it (to any other name that does not present in the package)
or you can use fully qualified import statement
I have the same problem finally,
I was solved.
//Use like this
import com.test.helpers.A;
If u have a more than class in com.test.helpers then u can use import com.test.helpers.*;
If you are not using an editor with auto-save, it might be helpful to check if the code you have written has actually been saved to the java file previously created
The bad source file error may be wrong if it is preceded by another error (below) which, in my case, provided guidance for how to fix the real problem. From my log file:
Error 1: a\g\GoodClass error:duplicate class: a.g.GoodClass //Not the problem
Error 2: a\b\BadClass error: cannot access GoodClass //The problem
bad source file: a\g\GoodClass //No, it's fine
file does not contain class x.y.GoodClass //How to fix it
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
Java reports the first line of Error 2 because BadClass is using a wildcarded import, either import x.*; or import x.y.*;.
THE FIX: remove the wildcarded import from BadClass and add only the specific imports you need from library x.y.
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.
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.