can't get Java import to work - java

I am trying to read and write modifications to an XML file. After much research I selected the JDOM approach for this activity. I'm not doing big files and I just need something quick and simple. I downloaded the JDOM project files from jdom.org/downloads/docs.html. I also downloaded some exmple files that show the read write code using JDOM. Piece of cake Right?
Wrong. I have screwed around for several weeks trying to get my Java JDOM project to work. Using Eclipse on Windows 7 PC. I can not get the import statements to actually import the required JDOM items.
I have my project files in S:\Java\procXML folder. The source files are in the S:\Java\procXML\src\procXML folder.
The JDOM files in my S:\Java\JDOM folder. The JDOM java source files are in the S:\Java\JDOM\core\src\java\org\jdom2 folder.
I have the CLASSPATH set to S:\Java\JDOM\core\src\java.
I get an error message from Eclipse saying the org.jdom2 imports cannot be resolved.
The import section of the procXMLfile is as follows:
package procXML;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
public class procXMLfile {
public static void main(String[] args) {
// TODO Auto-generated method stub
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("c:\\file.xml");
This stuff should be simple. The reason I'm using java is because I need to write some programs for MAC and Android and Java can be compiled for all of those platforms. I'm about ready to get rid of java and go with so other language, so I'm hoping someone can tell me what is wrong.

You are adding a source folder in your CLASSPATH.
Java classpath doesn't know how to interpret your source project.
You could compile your code through javac or via Eclipse to get a JAR file.
But if you just want to use JDOM in your project (which seems to be the case here), the simplest way is to add already packaged jar to your CLASSPATH:
Download the latest binaries from JDOM
Unzip them in a folder, for instance S:\Java\procXML\lib
Add in your classpath the jar inside the lib folders: S:\Java\procXML\lib\jdom-2.0.6.jar,S:\Java\procXML\lib\lib\jaxen-1.1.6.jar, S:\Java\procXML\lib\lib\xercesImpl.jar, S:\Java\procXML\lib\lib\xml-apis.jar, S:\Java\procXML\lib\lib\xalan\serializer-2.7.2.jar, S:\Java\procXML\lib\lib\xalan\xalan-2.7.2.jar

Related

How to import java files from folders in linux?

In my linux, I have my java files that I coped from my windows eclipse project, and now I want to compile it in linux.
The folder structure is
PlutoMake.java
java-json.jar
Filter\ColorFilter.java
Filter\Darken.java
Filter\NoFilter.java
Filter\VividLight.java
The PlutoMake file has these imports and some others too like for json
import Filter.ColorFilter;
import Filter.Darken;
import Filter.NoFilter;
import Filter.VividLight;
But when I try to compile plutomake, it says
PlutoMake.java:12: package Filter does not exist
import Filter.ColorFilter;
I already use this to compile it:
javac -cp "java-json.jar" PlutoMake.java
and similarly for other ones too.
Does anyone know how to import it?
Thanks
You are getting package Filter does not exist error because the Filter.* classes are not present in the class path.
First compile the java files in side the folder Filter then compile the PlutoMake.java using javac -cp "java-json.jar;." PlutoMake.java
I'll advice to use some build tool like Maven or Gradle or Ant
If i understand the question correctly. In eclipse just go to fil > Import > Existing Projects into Workspace
Select your project and import all your files. Then hit finish.
Are they in the right subdirectories?
If you put /usr/share/'classpath', files defined with package Filter should be in /usr/share/Classes/Filter/
try setting java -classpath

Java OFBiz can not find symbol import java.io.FileOutPutStream and package org.apache.commons.fileupload does not exits?

I want to user from file upload in Java OFBiz but when i do the following error occur:
import java.io.FileOutPutStream;
import org.apache.commons.fileUpload;
and other io, and apache packages bellow error occur:
Can not find symbol import java.io.FileOutPutStream Symbol: class FileOutPutStream
and another error
package org.apache.commons.fileupload does not exist.
I use eclipse , and the following is structure of path that code stored:
hot-deploy->my_project->src->file.java for Java files
hot-deploy->my_project->widget->myScreens->fileUploadScreens.xml
'hot-deploy->my_project->widget->myForms->fileUploadForms.xml'
But their is no lib folder inside WEB-INF
Your import statements are wrong, they should be
import java.io.FileOutputStream;
import org.apache.commons.fileupload.*;
I recommend to set up a new OFBiz module using the ant target
./ant create-component
and answer the following questions. This will install a skeleton module in hot-deply and will be able to access everything OFBiz delivers OOTB.
You should also check if the OFBiz .project file for Eclipse is read correctly. You should see a lot of source (src) folders and libraries in the project properties/build path of your project. if not, your .project file is not recognized.

package org.pdfbox.cos does not exist

I am trying to import and use PDFBox, but am having issues installing the jars I believe.
I am using Dr. Java, and I have added both the pdfbox-1.8.6.jar and the pdfbox-app-1.8.6.jar to my resource locations in my Extra Classpath.
However, it still is not recognizing the jars when I run the code:
import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;
What other steps do I need to do in order for the jars to work?
Use the correct imports:
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdfparser.PDFParser;
Additional hints:
pdfbox-app-1.8.6.jar contains the classes from pdfbox-1.8.6.jar, so you don't need pdfbox-1.8.6.jar too
if you want to load a document for rendering or text extraction, you won't need these two imports. Loading is done with
import org.apache.pdfbox.pdmodel.PDDocument;
PDDocument doc = PDDocument.loadNonSeq(new File(filename), null);
To see how it is done, look at the examples in the source code. Good luck!
Have you tried the steps detailed on the documentation:
Adding And Removing JARs in DrJava
A JAR file is a way of storing many (pre-compiled) Java classes. Below
are instructions for adding and removing jar files from DrJava's
resource locations. For more information on JAR files, see our JARs
page and Using JAR Files: The Basics on Sun's site.
How to add a JAR
When you download the jar, keep track of where you save it to
Open DrJava
Edit > Preferences
Resource Locations (at left)
In Extra Classpath, Add (browse and chose the new jar)
Apply
Okay
Quit DrJava and re-open it in order for the change to take effect

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.

Import library is not resolved

I have tried to compile a java program, shorten.java from mp4parser site. But my jar file is unable to resolve the required import shown below.
This program needs to import library from iso mp4parser as mentioned below. In short I need jar file for shorten.java file to resolve these import.
import com.coremedia.iso.IsoFile;
import com.coremedia.iso.boxes.TimeToSampleBox;
import com.googlecode.mp4parser.authoring.Movie;
import com.googlecode.mp4parser.authoring.Track;
import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder;
import com.googlecode.mp4parser.authoring.container.mp4.MovieCreator;
import com.googlecode.mp4parser.authoring.tracks.CroppedTrack;
I have downloaded the source code from the mp4parser site to create the jar file for these library.
In below image java->com->coremedia->iso and java->com->coremedia->iso->boxes having required java program
and java->com->googlecode->mp4parser->authoring etc. having other needed java program for these import
I have created the jar by following command-
cd java // moved in java folder
jar cfv mp4parser.jar *
now add these jar to my shorten.java program.
But I am getting error that import is not resolved.
****Do anybody can tell me where I am doing mistake ???****
Here is the directory where java codes for these imports are available.
I suggest you to this:
Create a quickstart java project with maven;
Add the dependencies that you want;
Put the java class in src/main/java
mvn clean install
If it don't work, try some IDE, like eclipse. Open the project and try to fix the imports... It's always possible that some lib has changed, and maybe they change the name of the package.

Categories

Resources