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

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.

Related

Java import from other file

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.

Do classes you import in java have to be saved already in separate files?

I have to write an extension code and am confused. Here's what I have:
import info.gridworld.actor.Bug
public class ZBug extends Bug
With the rest my code below that. I keep getting an error message saying that "the import info cannot be resolved" I'm not sure what I'm doing wrong
You are missing a semicolon at the very least:
import info.gridworld.actor.Bug;
Then, you may miss some of the dependencies.
Make sure that the info.gridworld.actor.Bug class or the JAR which contains it is in the project's build path.
If it is a Maven project, make sure to include the required <dependency>.

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

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.

Java using classes from jar

This must be a super overasked question. Although here goes:
I have a java file for testing around (hworld.java) and am trying to import conio.jar, a JAR which is a wrapper of Conio. The JAR contains only one class file (conio.class) and META-INF. Trying to do import conio.* or import conio.conio shows me this:
C:\Documents and Settings\Nick\Desktop>javac -cp *.jar; hworld.java
hworld.java:3: error: package conio does not exist
import conio.*;
^
1 error
And compiling it like javac -cp conio.jar hworld.java still errors out while compiling. I even extracted the jar and had conio.class in the same directory as hworld.java but to no avail. The JAR is in the same directory as hworld.java, as well.
Anyone have any idea on how to fix this?
You don't mention whether conio.class is defined in package conio. If it is not, then simply use the class without importing it. Remove the import.
It's actually not possible. You need to put the other class in a package if you want to import it.
What's the syntax to import a class in a default package in Java?
Find out what package Conio is in - an easy way to do this is to open the jar as a zip file, the package will correspond with the folder structure of the archive. For example if Conio is in x/y/z then import x.y.z.Conio and compile/run with conio.jar on the classmate.

Categories