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
Related
I want to use the StdDraw package, and I've tried many different ways of importing it.
Of course I tried:
import StdDraw;
But, when I look at the documentation from Princeton here, it shows that StdDraw is part of Objects, so I try this:
import java.lang.Object.StdDraw;
However, this results in an error:
error: cannot find symbol in
import java.lang.Object.StdDraw;
I saw this question here but it does not answer this question either.
How do I import StdDraw? thank you.
if you want to use StdDraw you must have
either the sources
or the classes (best zipped up as jar)
as preferred way you use the sources (see http://introcs.cs.princeton.edu/java/15inout/). it says there "To use these libraries, download StdIn.java, StdOut.java, StdDraw.java, and StdAudio.java into the same directory as your program. "
once you did this the imports should be working.
NOTE: all four files are not in packages, so you should 'download' them into the 'standard' package. That means you have to download them to the root package of your project.
by the way: don't import import java.lang.Object.StdDraw; but do just import import StdDraw;
First of all check encoding of your IDE. It should be set to UTF-8. It is important if you are using MS Windows operating system.
Then create StdDraw.java class in the same package as the package of your program you are writing. Remove class declaration, leave only package declaration.
Then visit this page: https://introcs.cs.princeton.edu/java/stdlib/StdDraw.java .
Copy all its contents (Ctr-A, Ctrl-C) and then paste it into StdDraw.java file you created previously.
StdDraw.java has its own main method so try to run it in order to check that the library works correctly. You should see a window with four strange figures :) .
Don't touch StdDraw.java anymore. Now you can easily import StdDraw library and refer to its methods with name of the class.
Enjoy
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.*;
I have searched and read many similar questions here, but I could not figure it out using solutions others have posted. Hence the need to ask a new question.
I downloaded Apache Commons Math. I then installed the jar file to my libraries of the project I am working on (in Netbeans):
So in RSA.java, I would like to use the nextLong method, as seen here:
I have tried numerous import statements, such as:
import commons-math3-3.4.1.*;
or
import docs.apidocs.org.apache.commons.math3.*;
But regardless, it gives me "package does not exist" no matter what I try:
Use the javadoc to locate the correct name of the class
import org.apache.commons.math3.random.RandomDataGenerator;
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
I have a lot of java files in my project so I decided to create packages and organize those files. After putting them in seperate packages like
com.myproject.android.activity
com.myproject.android.adapter
etc...
eclipse wants me to import the R file. From different SO questions I know "Never import the R file". However without that eclipse shows error messages that R.java is missing and wants to import it.
I already did a eclipse restart, clean, and android->fix with no success. My resource files are without errors. Is it safe to import the R file. Any suggestions?
Yes, you can import R files, they are sometimes needed to be imported, e.g. when you define them in a library and you need to use them in the package that uses that library.
You can also use full names - this may make the code more clear even if longer.