Java import from other file - java

I'm a bit of a beginner in Java and I have an aissgnment to do and was given a code to edit.
Problem seems to be that there is an error in including the created packages. The import looks like this:
package de.cogmod.anns.exercisesheet01;
import java.util.Random;
import de.cogmod.anns.exercisesheet01.misc.LearningListener;
import de.cogmod.anns.exercisesheet01.misc.Tools;
with the folder structure being as the import statements declare it. However the following error message is shown when i try to compile it with javac on windows.
package de.cogmod.anns.exercisesheet01.misc does not exist
I guess there is a very simple solution to this but I just can't figure it out. Thank you very much!

I will not give you clear answer since this is an assignment. But here are some pointers.
javac works on a single class if not instructed to run recursively.
classpath is very important during compile time and runtime.

Related

use java packages in databrick scala

I'm new to databrick and I'm just trying to copy my scala code to databrick notebook.
However, for the imports, everything is fine except
import org.apache.spark.util.LongAccumxulator
which is a package written in Java,
I know that scala can import java packages, so what is going wrong here?
The error message is:
notebook:11: error: object LongAccumxulator is not a member of package
org.apache.spark.util import org.apache.spark.util.LongAccumxulator
What is the reason for getting this error?

Getting Was expecting: <NAME> ... when importing java file in jython

I am quite new to jython. And in my project there is an existing jython file.
This jython file is referring to few java classes present in custom jar.
Here are the import statements
from oracle.ess import PrintVersion
from oracle.as.scheduler import CalendarWrapper
first import statement is not giving any error
but second import statement is giving error under as which is part of package name saying was expecting: NAME ...
One more thing both import statement are part of same jar, so I can not think of any classpath related issues.
So I am feeling like as is not allowed word as part of java package name.
Is there some thing like not allowed words in java package name while using them in jython.
Any kind of help will be appreciated.
as is a keyword, check it with keyword.iskeyword(s):
import keyword
keyword.iskeyword('as')
This makes it impossible to import any module named as.

How to import correct package for StdDraw?

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

Trying to import RXCardLayout

Trying to import import GUI.layouts.RXCardLayout; but it can't be resolved. It is an extension of CardLayout that someone made and I found it online. Anyone know what the right import is?
I assume you are talking about this RXCardLayout.
There are no special imports. It doesn't use a package. You just add the source to you project and compile the source code into your class path.
Maybe the problem is you need to add a package statement to put the code into the same package as your other source code.

Import custom java file

I'm currently trying to read some source code in Java I found online to study and learn the material. I want to compile the files first to make sure they work before I study it. When I try to compile though, the compiler complains that it can't find some of the files it needs to import. So opening up the main.java, I find
package br.com.seimos.minijava;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import br.com.seimos.minijava.parser.MiniJavaParser;
import br.com.seimos.minijava.syntaxtree.Program;
import br.com.seimos.minijava.visitor.TreeDumper;
import br.com.seimos.minijava.visitor.TreeFormatter;
The errors I'm getting are coming from not being able to find MiniJavaParser, TreeDumper...the 3rd chunk of code. Those files exist in the same directory as the main file though, so what is going on? What is br.com.seimos.minijava stuff? I tried putting the files in that those folders (as in br\com\seimos\minijava\PUT_FILES_HERE) but still no good. Does br need to be in the root directory?
Thanks, I realize this is probably a really elementary question...
The required directory structure is br/com/seimos/minijava/OTHER_FOLDERS/SOURCE_FILES.java. For instance, the path to MiniJavaParser should be br/com/seimos/minijava/parser/MiniJavaParser.java. You should then run the Java compiler from the parent directory of br.
If you're using Eclipse or another IDE, you should configure your project settings to handle this.
For import br.com.seimos.minijava.parser.MiniJavaParser; your MiniJavaParser class must be in the directory br\com\seimos\minijava\parser\ and not br\com\seimos\minijava\. Similarly for other classes. Try changing it.
You're on the right track. You will need to put those files in br/com/seimos/minijava/... as indicated by the package name of each. Java requires that you put files in a directory hierarchy that matches their package names.
Then, you'll want to compile using a command like:
javac br/com/seimos/minijava/parser/MiniJavaParser.java
This is all a bit inconvenient from the command line, especially for a larger project, so you might be better of getting a Java IDE and having it help you arrange the files.

Categories