Packages in java not compile - java

Good morning everyone!
I did a project x but all my files were out of order so I decided to group them by folders.
foldera
---ClassA.java
---ClassB.java
folderb
---Class1.java
Main.java
The problem arises when I try to compile, since in the main it appears that the classes I made are not found
I thought this could be solved by putting in the classes
package src.foldera.ClassA;
And in the others the same
package src.foldera.ClassB;
And
package src.folderb.Class1;
So in all classes
And in the main put
import src.foldera.*;
import src.folderb.*;
But I keep getting the same error even though I put the packages
It should be noted that I did not create the folders in the code editor, rather I did it in the same Windows 10 File system
What is this about? Thanks!

Assuming a standard setup where the src folder is the root of your source hierarchy, the statements should be:
package foldera;
package folderb;
The imports should be similarly shortened to:
import foldera.*;
import folderb.*;

You have to define the package in which the class is in.
As an example in classA:
package src.foldera;
And in class1:
package src.folderb;
import src.foldera.ClassA;

Related

How to import a class inside one package into another class in a different package?

I have to solve an exercise related to java packages. The hierarchy and the package names are already created with the question and cannot be changed.
Project
|__src
|__main
|__java
|__code
| |__numbers
| |__Numberfinder.java
|__com
|__keith
|__Main.java
I have to use Numberfinder class in Main.java. So in Main.java I wrote:
in Main.java -
import java.code.numbers.Numberfinder
But this was highlighted in red and showed error as Numberfinder not found.
I also tried : import code.numbers.Numberfinder in Main.java
But it did'nt work either.
Kindly suggest me what to do.
import code.numbers.Numberfinder; is the correct import. If you are having issues with that you need to mark src/main/java as the source directory

Java compiler doesn't see sibling packages

From Windows command line, I am trying to compile a java file in package com.example.web that imports a class from package com.example.model but keep getting the message that the package com.example.model does not exist.
From what I can tell, the compiler doesn't recognize sibling packages for some reason, as I do not get the error message when I try to import the package com.example but I do get it if I try to import com.example.test. I've compiled the files in the other packages that are not dependent on imports without an issue from the same directory. I'm almost positive that I've had access to sibling packages from the command line in the past.
package com.example.web;
import com.example.*;
import com.example.test.*;
import com.example.model.*;
public class Test{
}
I get 2 errors saying that package com.example.model and com.example.test do not exist. They're in my file structure, they have .class files in them.
UPDATE: It works if I add com.example.model and com.example.test to my classpath, but I was under the impression this was unnecessary if the dependencies shared the same parent folder as the class I was compiling. Am I mistaken? Did this use to be the case but changed in the last few years? It's been awhile since I've compiled directly from command line, but I don't remember having to do this before.

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