Running .class file from cmd [duplicate] - java

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 8 years ago.
Thats code:
package helloWorld;
public class helloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
What's wrong? I am pretty newb.

You need to specify the classpath:
java -cp C:\Users\jklh\Desktop\java_files\helloWorld\bin helloWorld.helloWorld

Related

Cannot Use Another Class in Java [duplicate]

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.

Error: Could not find or load main class Check.java [duplicate]

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

Could not find or load main class when running Java against a class file [duplicate]

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

How this program compiled and run successfully? [duplicate]

This question already has answers here:
Java Label usage [duplicate]
(2 answers)
Closed 6 years ago.
The below program compiled successfully and run without any errors. As per my understanding is should have thrown error in line 4. Can somebody explain?
class Test{
public static void main(String args[]) {
// my favorite website is
http://www.stackoverflow.com/questions/ask
System.out.println("hello world");
}
}
No, it's not an error, "http:" works here as the name of the label and "//" starts the comments which is ignored.

Website name in java program [duplicate]

This question already has answers here:
My java code has an obvious error. Why does it compile and run?
(2 answers)
Why doesn't putting a random http link in code cause a compilation error? [duplicate]
(2 answers)
Closed 9 years ago.
public class Main
{
public static void main(String[] args)
{
http://google.com
System.out.println("Welcome");
}
}
Why is this code not getting a compilation error? and why did it get a compilation error if we write this url name after System.out.println("Welcome"); ??
Because it's a label followed by a comment.

Categories