This question already has answers here:
Java: Input strings with keyboard class
(5 answers)
Closed 9 years ago.
Im a newbie on Java. I get an error. These are my simple codes:
public class AreaRect {
public static void main(String[] args) {
int height, width, area;
System.out.print("yukseklik?");
height = Keyboard.readInt();
System.out.print("genislik?");
width = Keyboard.readInt();
area = height * width;
System.out.print(area);
}
}
and I get this error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Keyboard cannot be resolved
Keyboard cannot be resolved
at AreaRect.main(AreaRect.java:8)
I have a Keyboard.class file in the same directory with AreaRect.java..
Please can you tell me where am I wrong?
You need to organize your files into packages.
Move both Java files to a folder, e.g., mypackage. Then, add the following line to the top of each file:
package mypackage;
You can also have subpackages with nested folders. For example, the folder structure:
com
example
mypkg
AreaRect.java
Keyboard.java
could use the package com.example.mypkg.
If you are trying to compile your class via commandline with javac AreaRect.java, it will work to have the Keyboard.class file in the same folder.
If you are however trying to run this in Eclipse, it won't work until you add the .class file to a seperate folder and then add said folder to your buildpath via "Properties->Java Build Path->Add Class Folder".
Related
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.
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 is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am at a very introductory level of programming with java. I am writing a simple code that involves taking an investment and adding an intrest rate into it. The code is not finished yet but I hav run into the java.lang.ClassNotFoundException error. Since I am so new to java, I have not yet run into this problem before. Where my confusion comes in is the program will compile. I really don't know how to approach this problem.
As I said I dont have a clue about where to start on this, here is the error.
Exception in thread "main" java.lang.NoClassDefFoundError: CDCalc/java
Caused by: java.lang.ClassNotFoundException: CDCalc.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
The entire code
import java.util.Scanner;
public class CDCalc
{
public static void main(String args[])
{
int Count = 0;
int Investment = 0;
double Rate = 0;
Scanner userInput = new Scanner(System.in);
System.out.println("How much money do you want to invest?");
int Invest = userInput.nextInt();
System.out.println("How many years will your term be?");
double Term = userInput.nextInt();
System.out.println("Investing: " + Investment);
System.out.println(" Term: " + Term);
if (Term <= 1)
{
Rate = .3;
}
else if (Term <= 2)
{
Rate = .45;
}
else if (Term <= 3)
{
Rate = .95;
}
else if (Term <= 4)
{
Rate = 1.5;
}
else if (Term <= 5)
{
Rate = 1.8;
}
System.out.println("Total is: " + (Rate * Invest));
}
}
I would greatly appreciate any help on this. Thanks
edit
I apologize, I should have included this. The code did compile just fine, the problem came in when I ran it. I did use javav CDCalc.java and java CDCalc and thats when the error came up. The even stranger thing is I didn't change a thing, closed out terminal and my text editor, deleted the saved files, reopened everything, saved it, compiled it, and it runs fine now. I apologize again for this post but it seems it fixed itself! –
Your code should compile fine. The way you compile Java files is different than how you execute them.
You compile with
javac CDCalc.java
...and run them with
java CDCalc
Looks like you tried to run it as java CDCalc.java, but it should be just java CDCalc.
The Java command takes a class name (not a file name), so there is no ".java" at the end, and no slashes or backslashes but dots for the package name (your class does not have a package).
This is an sample example:
public class HelloWorldDemo {
public static void main(String args[]) {
System.out.println("Hello world test message");
}
}
This is sample hello world program,When i compile this program using javac HelloWorldDemo.java command, this compiles fine and generates HelloWorldDemo.class in the current directory,
After running this programm using java HelloWorldDemo command, I am getting the below exceptions.
Exception in thread "main" java.lang.NoClassFoundError: HelloWorldDemo
thread main throws this error and exit the program abnormally.
This reason for this error is java virtual machine can not find class file at run time. java command looks for the classes that are there in the current directory, so if your class file is not in current directory, you have to set in classpath, so the solution is to place this .class file in the classpath
classpath is the enviornment variable in every system which points to class files in the directories. if you classfile is in jar file, jar should be in classpath. classpath can be absolute(complete path) or relative path( related to directory )
solve java.lang.NoClassDefFoundError :-
HelloWorldDemo.class is not avialble at runtime, so we have to set the class file to java command using -classpath option
java -classpath . HelloWorld
This is for fixing NoClassDefFoundError error by setting classpath inline for java command.
We are instructing the jvm to look for the HelloWorldDemo.class in the current directory by specifying .
if class file is in different directory, we need specify the complete directory absolute or relative path instead of . for java command
Fix for java.lang.NoClassDefFoundError in windows:-
To solve NoClassDefFoundError error in windows , we have to set CLASSPATH environment variable.
to set classpath in windows, we have to configure the below values
set CLASSPATH=%CLASSPATH%;.;
%CLASSPATH% means existing classpath to be added and . points to current directory
After setting classpath,
java HelloWorldDemo
command works fine and prints hello world message
Your program seems fine. Did you compile your java file using Javac?
You need to perform steps below:
Compiling the .java file (source code) using javac
javac CDCalc.java
This should create a class file named CDCalc.class
Executing the compiled class file using java
java CDCalc
This question already has answers here:
R cannot be resolved - Android error
(108 answers)
Closed 10 years ago.
I'm developing an android apps with Eclipse.
In my app, I try to read a file : data.xml. I put it in res/raw/, and to access it i'm supposed to use getRessources().openRawResource(R.raw.data);
But Eclipse show me an error : "data" cannot be resolved or is not a field.
But the field is in the gen/R.java !!!
public final class R {
public static final class raw {
public static final int data=0x7f040000;
}
}
Any ideas ?
Thanks
Solution :
Import the right R.java files !
import my_package.R;
Stop trusting ctrl+shift+O ...
I already faced this problem several weeks ago. You simply have to use com.example.R (where com.example is the name of your package), because your IDE thinks that you are using android.R by default.
Try this out.
Try to clean and rebuild your project!
Or just delete import android.R;.
If that data.xml is in raw folder but still its not resolved once Clean and build your project and check.
Still error check this : Opening raw file