Compiler couldn't read imported class [duplicate] - java

This question already has answers here:
How to compile java package structures using javac
(6 answers)
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 2 years ago.
I have been trying to compile through CMD by creating packages manually. I tried the command javac -d . Nameinfo.java and javac -d . Returndata.java, where Nameinfo contains the calculations and Returndata contains main function, and this command worked absolutely fine. Now I made 2 different packages in my "java programmes" folder i.e packx and packy, where packx contains Nameinfo and packy contains Returndata. Now, when I compile these two, Nameinfo.java got compiled but Returndata could not read/recognize the imported Nameinfo in Returndata.java.
errors:
E:\java programmes>cd packx
E:\java programmes>cd packx
E:\java programmes\packx>javac Nameinfo.java
E:\java programmes\packx>cd..
E:\java programmes>cd packy
E:\java programmes\packy>javac Returndata.java
Returndata.java:2: error: package packx does not exist
import packx.Nameinfo;
^
Returndata.java:7: error: cannot find symbol
Nameinfo data = new Nameinfo();
^
symbol: class Nameinfo
location: class Returndata
Returndata.java:7: error: cannot find symbol
Nameinfo data = new Nameinfo();
^
symbol: class Nameinfo
location: class Returndata
3 errors

if java or javac needs, say, packx.Returndata as a type to do something, it will scour a classpath and/or sourcepath for a directory named packx, and within that, Returndata.java or Returndata.class depending on the tool and path we're talking about.
So, you're in E:\java programmes\packy, and you're compiling some code that imports packx. Therefore, javac will look in the classpath for packx/Returndata.class and in the sourcepath for packx/Returndata.java and it doesn't find what you want because E:\java programmes isn't on the source path; generally they default to . (i.e. the current directory).
Stay in the E:\java programmes folder for all this work:
E:
cd "\Java programmes"
javac packx/Nameinfo.java
javac packy/Returndata.java
Or better yet, realize that manually compiling a multipackage java application is crazy. Use maven, gradle or some other build tool that takes care of all this for you :)

Related

java error: Could not find or load main class ass1.insert [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 6 years ago.
I have three java files dictionary.java postings.java and invert.java
The first line for all three is:
package project1;
The folder path for these are Desktop/cps/.
I have compiled the files using (while in the directory cps):
javac -d . *.java
... which creates a folder project1 with all three .class files.
when I try to run using the following command:
java project.invert
I get that error:
Could not find or load main class project.insert
How do i run invert.java?
The invert.java has the main class while the other two are just class definition files.
There maybe a typo in your execution command. It should be java project1.invert notice the 1 with project. Further the signature of main method must have String[] args argument.
It should be
public static void main(String[] args)
If the access specifier is missing then it will result in compile time error.
Hope this helps.
Folder path for all 3 files must be Desktop/cps/project1 instead of Desktop/cps/.
Packages in Java represent directories in the file system.
You must compile while being in Desktop/cps/ as javac project1/*.java
and run the program using java project1.invert

Java compiling .java with 'extends' fails

I am trying to compile the pentaho samples on OSX from here.
Sample1.java extends AbstractReportGenerator.java.
I have exported the CLASSPATH in the console to include the pwd:
export CLASSPATH=/Users/xxx/java-dev/pentaho/:/Users/xxx/java-dev/pentaho/lib/
I have compiled AbstractReportGenerator.java successfully using javac, into the same folder as the Sample1.java file, however when I try and compile Sample1.java it always fails with 3 errors related to AbstractReportGenerator.
Any suggestion on the cause and solution ?
Folder structure:
Working Folder >
- Sample1.java
- AbstractReportGenerator.java
- AbstractReportGenerator.class
- AbstractReportGenerator$1.class
- AbstractReportGenerator$OutputType.class
- lib
- pentaho .jar files
Command:
javac -cp "lib/*" Sample1.java
Sample1.java:41: error: cannot find symbol
public class Sample1 extends AbstractReportGenerator
^
symbol: class AbstractReportGenerator
Sample1.java:61: error: cannot find symbol
final ClassLoader classloader = this.getClass().getClassLoader();
^
symbol: method getClass()
Sample1.java:132: error: package AbstractReportGenerator does not exist
new Sample1().generateReport(AbstractReportGenerator.OutputType.PDF, outputFilename);
^
Note: Sample1.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors
Try using:
javac -cp lib/*:. Sample1.java
Thanks so much for all the suggestions, after adding a target folder, this suggestion from #Roman Vottner finally worked:
javac -cp ./lib/*:./target -d ./target AbstractReportGenerator.java Sample1.java

Yet another 'cannot find symbol' error when creating a new class object

In short, I'm trying to instantiate within the main method in order to handle computations. I wrote the main class in Eclipse and was able to compile and run everything smoothly.
Main Method:
public static void main(String[] args)
{
...
OutsideClass class = new OutsideClass();
...
}
I ran it in eclipse, which worked smoothly until I got an error due to to insufficient privileges, which led me to switch over to using cmd.exe as an administrator.
I navigated to the eclipse folder where I had all the classes saved to and ran javac x.java for each file in the folder, one by one. I was able to do javac OutsideClass.java without any errors, though when it came to javac Main.java, I received the following error:
Main.java:36: error: cannot find symbol
OutsideClass outside = new OutsideClass();
^
symbol: class OutsideClass
location: class Main
Main.java:36: error: cannot find symbol
OutsideClass outside = new OutsideClass();
^
symbol: class OutsideClass
location: class Main
2 errors
The OutsideClass doesn't have a defined constructor, though I don't know if that really matters or not.
The Java compiler needs the source (.java) or bytecode (.class) of OutsideClass when compiling Main.java.
Try javac *.Java, or javac -cp OutsideClass.class Main.java to provide OutsideClass's definition to the compiler when compiling Main.
It is more customary for Java developers to compile all Java sources of a single project via one javac invitation, either directly, or via a tool such as Maven.

Java Symbol not found through javac weird error (Runs through Eclipse)

My project runs smoothly in Eclipse, no errors at all. I ran it through local tests (that are equal to the ones in an online mooshak contest) and all checks out. But when I sumbit it online, I get a CompileTimeError.
To try to locate the problem, I attempted to use javac in Main.java, and this happened:
javac -encoding US-ASCII Main.java
Main.java:8: error: package code does not exist
import code.*;
^
Main.java:129: error: cannot find symbol
public static void addWords(Scanner in, LibSystem system, int wordCount)
{
^
symbol: class LibSystem
location: class Main
Main.java:153: error: cannot find symbol
public static void searchWord(LibSystem system, String word) {
^
Other symbol errors follow for every reference to classes from my only package, which I fully import and the header of Main, for a total of 29 errors.
Project
src
Main.java
code (package with all my other java files)
Follow these steps :-
1.) make sure LibSystem class is in classpath.
2.)Compile java files in code folder using -d option. That will create directories with classes.
javac -d . code/*.java
3.) then compile your Main class.
javac Main.java

Cannot find symbol (extends another class)

I have my file structure as (they are in the package assigment):
prog/
src/assignment/
bin/assignment/
lib/assignment/
The files I am compiling are in src, and are being compiled to bin. The command I type is javac -d ./bin/ -cp ./bin/:./src/ -target 1.7 ./src/assignment/*.java
I have also tried javac -d ./bin/ -cp ./bin/:./src/ -target 1.7 ./src/assignment/GoodBrain.java ./src/assignment/LameBrain.java ./src/assignment/Board.java
For some reason I cannot two class files, the error message is 'cannot find symbol' (it points to the Board class and the LameBrain class).
The error message:
javac -g -d ./bin/ -cp ./src/:. -target 1.7 src/assignment/GoodBrain.java
src/assignment/GoodBrain.java:3: error: cannot find symbol
public class GoodBrain extends LameBrain{
^
symbol: class LameBrain
src/assignment/GoodBrain.java:11: error: cannot find symbol
public double rateBoard(Board board) {
^
symbol: class Board
location: class GoodBrain
2 errors
make: *** [bin/assignment/GoodBrain.class] Error 1
It is the same error for all the commands that I type in. Please tell me if I need to provide any new information
I fixed the error, it was because I forgot to include the package statement. Very dumb mistake

Categories

Resources