This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 9 months ago.
I have this problem where I can't use another class inside the same package. Here is the code:
package Main;
public class App {
public App() {
new GUI();
}
public static void main(String[] args) {
new App();
}
}
And here is the command that I use to run the application:
cd "c:\Save_File_Program\Java\FinalProject\src\Main\" ; if ($?) { javac App.java } ; if ($?) { java App }
App.java:5: error: cannot find symbol
new GUI();
^
symbol: class GUI
location: class App
1 error
I do have GUI.java inside Main package, same as App.java.
This problem occured after I tried to connect mysql with java using JDBC.
The steps that I followed is:
download mysql-connector-java.jar, copy the jar to "C:\Program Files\Java\jre1.8.0_331\lib\ext\mysql-connector-java-8.0.29.jar", and make a new system variable named "CLASSPATH" with a value to the jar path.
Any help is greatly appreciated. Thank you.
I think the problem is, that your code references a class from a different file, that javac does not know about. It should work if you call javac with all required files. In your case either javac *.java or javac App.java GUI.java Menu.java.
Related
I have created a simple java project "DesignPatterns".
I created a file FacadePattern.java with the path being ~/DesignPatterns/Structural/FacadePattern/FacadePattern.java
My FacadePattern.java class,
public class FacadePattern {
public static void main(String args[]) {
// Some code
}
}
But my editor (VSCode) gives me an error at the start of the file:
The declared package "" does not match the expected package "Structural.DecoratorPattern"
So upon clicking on quick fix, it added package Structural.FacadePattern; in the first line.
So the final code became
package Structural.FacadePattern;
public class FacadePattern {
public static void main(String args[]) {
// Some code
}
}
But when I compile the above file using the command
javac FacadePattern.java && java FacadePattern
It is giving me the following error:
Error: Could not find or load main class FacadePattern
Caused by: java.lang.NoClassDefFoundError: Structural/FacadePattern/FacadePattern (wrong name: FacadePattern)
After searching a lot in the internet, I ran the following command:
javac -sourcefile ~/DesignPatterns FacadePattern.java && java FacadePattern
But no use, I am getting the same error.
Can anyone explain why my editor gave an error but code ran successfully before? and why wont it compile after adding "package Structural.FacadePattern;" line? and what is -sourcefile parameter in javac command? and how to run the code with successfully without errors in editor as well as the terminal?
Stick to naming rules. Package names should be lowercase only. In your case, it should be package structural.facadepattern;
Run the 'correct' class. Because your class is not inside 'the base folder' aka the 'default package', you have to prefix the class with its package name: java Structural.FacadePattern.FacadePattern or rather java structural.facadepattern.FacadePattern if you use the proper case for packages.
This question already has answers here:
How to run a .class file that is part of a package from cmd?
(13 answers)
Closed 2 years ago.
I have Check class in my ua.kiev.prog package.
It is Checkclass :
package main.java.ua.kiev.prog;
public class Check {
public static void main(String[] args) {
System.out.println("Hello world");}
}
I need to run program from console and I am trying in console :
C:\PROJECTS FOR FOXMINDED\CHECK\src\main\java\ua\kiev\prog>javac Check.java
And when I am trying to run :
C:\PROJECTS FOR FOXMINDED\CHECK\src\main\java\ua\kiev\prog>java Check.java
Error: Could not find or load main class Check.java
In my system variables I have :
CLASSPATH : D:\Java\jdk1.8.0_202\lib
PATH : D:\Java\jdk1.8.0_202\bin
Appreciate for each help!
The (qualified) name of your class is main.java.ua.kiev.prog.Check, not Check.java, so use java main.java.ua.kiev.prog.Check instead. The java command doesn't and cannot do anything with source files.
Make sure you're running from C:\PROJECTS FOR FOXMINDED\CHECK\src
This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 3 years ago.
I have this code:
package com.myjava;
public class MyClass {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
Then
javac MyClass.java
Then
java com.myjava.MyClass
But throws
Error: Could not find or load main class com.myjava.MyClass
Why is it throwing that error, what could be wrong in the process?
It looks like you are running
javac MyClass.java
from com\myjava context where MyClass.java is located, and then running
java com.myjava.MyClass
which would be com\myjava\com\myjava\MyClass.class, which doesn't exist.
Hope that helps!
You don’t need to specify the package information when running the class so it would just be:
java MyClass
As long you are in the right directory it will work, if you are not, you must specify either the absolute file path to the class file or the relative path from your current location.
So if you were in the root directory of the package it would be:
java com.myjava.MyClass
This question already has answers here:
How to execute a java .class from the command line
(7 answers)
Closed 6 years ago.
I have a seemingly simple program in Java, but when I run it, I get the error:
Error: Could not find or load main class
Here is my code.
public class HelloPrinter
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
I'm entirely new to the Java language, and I don't know what else to do. My program file is named "hello_world.java", and when attempting to run the program, I type in "java hello_world.java". Am I doing something fundamentally wrong? I've also attempted "java -cp hello_world", but that gave me the same error.
What am I doing wrong?
From what little information you've given, I'd say you're executing it the wrong way.
Make sure you run the following two commands:
javac HelloPrinter.java
java HelloPrinter
The first command compiles your source code into a .class file. The second command executes that class file.
This question already has answers here:
Java Access Denied
(3 answers)
Closed 8 years ago.
I use Windows 7 and recently installed JDK (jdk1.8.0_05). (I've also installed JRE)
As I'm new to Java, I complied the Hello World Java code given on Official Java Website:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
I saved the file as C:\HelloWorldApp.java
And then in command prompt, I typed
javac HelloWorldApp.java
I got the following message:
C:/HelloWorldApp.java:5: error: error while starting HelloWorldApp: C:\HelloWorldApp.class (Access is denied)
class HelloWorldApp{
^
1 error
PS: I even tried making the class public. Also, I've specified PATH variable as C:\Program Files\Java\jdk1.8.0_05\bin
What may be the solution?
Add public keyword before the class keyword. That is, change:
class HelloWorldApp {
to
public class HelloWorldApp {
The class that contains main method must be declared as public.