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.
Related
I am working on a Java fx application and I need to import some libraries from com.google.maps.
I imported these libraries :
import com.google.maps.GeoApiContext;
import com.google.maps.PlaceDetailsRequest;
import com.google.maps.PlacesApi;
import com.google.maps.QueryAutocompleteRequest;
import com.google.maps.errors.ApiException;
import com.google.maps.model.AddressComponent;
import com.google.maps.model.AddressComponentType;
import com.google.maps.model.AutocompletePrediction;
import com.google.maps.model.PlaceDetails;
The java import statement is a little bit misnamed, it really means alias. import com.google.maps.GeoApiContext; really just means: Any time you find the type GeoApiContext anywhere in this source file, assume I meant to write com.google.maps.GeoApiContext.
Crucially, it does not 'invoke' any code in that class whatsoever, nor does it find or download any dependencies from the internet for you.
You will need to find the jar(s) that provide these classes and put them on the classpath of this project.
It can be as simple as downloading the relevant jar (perhaps com.google.maps-google-maps-services.jar?), put it in a lib dir someplace inside this project, finding that in the package explorer, right clicking it, and selecting 'add to classpath'.
Or, more likely, you want to use gradle or maven to take care of this for you: These tools turn simply mentioning the dependency in a list of libraries you require into automatically finding that on the internet, downloading it, configuring your IDE so that it knows where it is, and using that dependency during build and run steps.
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.
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
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.
I am very new to Java.
I would like to use some features from package called daj
The tutorial code has the following lines.
import daj.*;
import java.util.*;
import java.lang.Math;
import Msg;
But first and fourth lines produce red underscore such that program cannot compiles.
How can I resolve this problem?
You have to add the packages to the classpath. If you are using an IDE as Eclipse or Netbeans, go to the project options and find the "Add library/jar" and they will take care of you.
You will need to add these libraries to the classpath both to compile and to run your program.
Also, the usual way of ordering imports is:
first, java. packages
second, javax. packagaes
third party packages
your packages
You need to have them in your build path.
That daj package or jar file should be in your package or current directory or in classpath.