ImportPackage com.itextpdf into a Java Class in XPages - java

I have created a new Java Class which at this point simply attempts to
package ca.wfsystems.core;
importPackage(com.itextpdf);
public class PrintPDF {
}
however I have an error on the importPackage line:
Multiple markers at this line
- Syntax error, insert "EnumBody" to complete EnumDeclaration
- Syntax error, insert "enum Identifier" to complete
EnumHeaderName
I believe I have the jar properly installed and it shows up in the package explorer under the WebContent/WEB-INF/lib/itextpdf-5.5.6.jar
I was using the example by Declan at http://www.qtzar.com/7plcn79gsvsw/
I have done a bit of Java but still on the steep part of the learning curve.

Of course the syntax has to be correct as stated by the other answers.
Then you could add the jar as an OSGi plugin which is what I am doing now. It is a lot cleaner to manage and work with - and I guess that is "the way" do do it in the Java world.
I wrote a couple of articles about how to wrap a jar as a plugin and how to install it on the server and in Designer :-)
/John

You don't import packages using importPackage: that is not even a Java keyword. If you want to import everything in a package, replace that line with:
import com.itextpdf.*;
Note that there is no notion of hierarchical relationship between packages in Java, so if you also want to import something from a subpackage, you need to import that subpackage explicitly:
import com.itextpdf.name.of.subpackage.*;

Related

Solrj Imports - package not found

I am trying to use SolrJ in Netbeans for my java application.
In the project library, I imported all the SolrJ java files :
org.apache.solr.client.solrj
But in my code, when I add :
import org.apache.solr.client.solrj;
It doesn't work and it says: package does not exist.
I have tried several methods but the package is never found.
What am I doing wrong here?
First, I don't think you can write the import that way. It should be more like this:
import org.apache.solr.client.solrj.*;
I don't know if Netbeans has an auto-import feature (have never used this IDE), but you might try to see if you can get Netbeans to auto-import something from solrj. Try SolrServer or QueryResponse.
Also, are you using Maven? Maven should make all this a little easier for you.
If that fails, I'm not sure what it could be, but here's a link to one or two people who had a problem very similar to yours: https://netbeans.org/projects/cnd/lists/users/archive/2007-12/message/86.

Eclipse type / import error with built java file

I have a project where we use a ant buildfile to create some .java files. Those built files need to reference to already existing other java files.
My problem is that the built file can have a code line as for example
arg = (Expr) new Ast.BinaryOP(lhs, BOp.B_MINUS, rhs);
where it will show an error "Ast.BinaryOP cannot be resolved to a type". I then can type Ast.BinaryOP again and use autocomplete to pick the BinaryOP part and the error will disappear. The same with BOp.B_MINUS.
I did import (probably way too many times) the necessary other files as far as I can tell.
I.e.
import cd.ir.Ast;
import cd.ir.Ast.*;
import cd.ir.Ast.Expr;
import cd.ir.Ast.BinaryOp;
import cd.ir.Ast.BinaryOp.BOp;
So far I tried
restarting Eclipse
'clean' the project
to just run the project anyway (doesn't work)
remove the "Ast." part which will work once again if I do it in Eclipse but not if generated that way
Google the problem, respectively search it on Stackoverflow which turned out to be hard as there are many related issues. There are no good keywords as far as I can tell.
deleted the project from Eclipse and imported it newly
I need to repeatedly run the ant build file to test the code from where the .java file is generated, thus changing all errors manually is not an option.
Thanks for any advice/help in advance. I will gladly provide more information if you tell me what could help.
Hit F5 on the project, open the file, and then try hitting Ctrl+Shift+O to automatically organize the imports.
After consulting with a friend who didn't see the issue either I finally found it. So logical in retrospect but small enough that I spend 2,5h trying to find it:
import cd.ir.Ast.BinaryOp;
vs:
new Ast.BinaryOP(....
BinaryOp vs. BinaryOP

Importing anything from "net" in eclipse

I am relatively new to java as a disclaimer. I see all kinds of code examples where people "import net...." yet anytime that I try to import anything from this directory, I get an error that the compiler cannot resolve the import net. What have these other programmers done that allows them to use this import? I have seen other people having this problem but I have not seen a straight-forward answer to this question.
For instance:
import net.sourceforge.binge.Xbox360Controller;
When you import an external package, you need to have it in your program during runtime, which means that it either has to exist in YOUR project, or if it is being loaded by another program, then that program has to contain it. To add a library to your eclipse project: see THIS

Import Error in Java Using Ant but not Eclipse

I can compile my program just fine if I specify sublibraries of com.itextpdf.*, but for some reason, my compile fails if I simply do import com.itextpdf.*. But, like I said, if I specify the sublibraries I'm using, such as
import com.itextpdf.text.Font.*;
import com.itextpdf.text.pdf.*;
Everything compiles just fine. In the case where it fails, the error I'm getting is "package com.itextpdf does not exist", but I know it does. I include it properly in the build path, and Eclipse doesn't give any compile errors in any case. The error only comes up when I try to build with Ant.
At this point, I don't mind specifying the sub libraries I'm going to use to make sure I get a build. But I am curious, why wouldn't Ant allow this?
You, and Eclipse, think of java namespaces as a hierarchy. However, in the Java specification, they are not. There is no relationship between "com.itextpdf.text" and "com.itextpdf.text.Font". They are different strings, end of story.
As a matter of style, you may want to set up Eclipse to automatically organize your imports and explicitly specify every class you use in the import section of your class. This avoids ambiguity when "com.itextpdf.text.Font.String" and "com.itextpdf.text.pdf.String" both exist, but you only want to reference one of them. This style would be incredibly tedious and inconvenient if you were programming in emacs or vi, but with Eclipse it is automatic and invisible, unless you scroll to the top of your file.

The declared package "com.example.android.softkeyboard" does not match the expected package "your.test.com.example.android.softkeyboard"

Im recieving the title error on several lines I used a example to create a application I did create a folder for .com.example.. and placed it in the root directory but it did not work I tried editing the Java script manually and it also did not work..
From what I can tell, it sounds like the top of your class with the compile error you mentioned has a package declaration like the following:
package com.example.android.softkeyboard;
However, the package that actually contains the class is as follows:
your.test.com.example.android.softkeyboard
You need to either change the package declaration line to look like the package directory structure or change the name of the package to look like the package declaration in the code. The latter option would be better because I suspect you've probably copied code from somewhere into your own package and that's why things aren't compiling. If you start renaming packages, you may cause more compile errors to appear before getting everything fixed. If you're new to Java, this can be very confusing to deal with.
If you are completely unfamiliar with how packages work, or if you'd like to read up on it, here are some good resources:
Oracle Java Package Tutorial
Wikipedia Java Package Article

Categories