Finding Main Class in "Hello World" program in Java [duplicate] - java

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.

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.

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

JAVA Console error [duplicate]

This question already has answers here:
System.console() returns null
(13 answers)
Closed 5 years ago.
If i run this code below in Netbeans
import java.io.Console;
public class Introduction {
public static void main(String[] args) {
Console console= System.console();// creates a java Object which has method that allows us to write
console.printf("Hallo parvel");
}
}
It gives me the error:
Exception in thread "main" java.lang.NullPointerException
at Introduction.main(Introduction.java:10)
/home/parvel/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds).
please help
java.io.Console object is null because there is no console attached to it.
Try to run from command line or attached console to it.
Find more details about it here: https://netbeans.org/bugzilla/show_bug.cgi?id=111337
You need to run this from a terminal using java -jar myjar.jar
In order to have this print to console using System.console();
You will need to export this as a runnable jar
then you can run java -jar myjar.jar and it will output Hallo parvel

Java Compile Error (HelloWorldApp); It shows "Error while writing" and "Access Denied" [duplicate]

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.

Getting "class does not have a static void main method accepting String[]" error even though main signature is correct

My DrJava was working fine, but now I keep getting the folowing error whenever I run anything:
Static Error: This class does not have a static void main method accepting String[].
So it will compile OK, but then it shoots out the error . This happens even though everything I test does indeed have a public static void main(String[] args) in it. It seems like a classpath/resources type of error. I appreciate any tips
EDIT: my class
public class Test{
public static void main(String[] args){
System.out.println(" hashmap ");
}
}
There's nothing wrong with the code, so the problem must be with the environment.
Check that you're actually executing that class. Find out where the class that's executed is specified and check it's correct
Check that you're compiling the class. Maybe the code you're looking at has not been compiled and you're trying to execute an old version that was compild before you coded a main()
Check your classpath. Is the compiled class accessible in the classpath of the java command
You don't need to reinstall java, nor is it a java version issue. It may be the way that your are running the program.
To check if it is a problem with your code, do the following:
Make a new folder and put Test.java in it.
Open up Command Line Or Terminal and change to that folder .
Type javac Test.java. Test.class should be in the folder now.
If you want, open up the class with a text editor. This is what I get:
˛∫æ2
<init>()VCodeLineNumberTablemain([Ljava/lang/String;)V
SourceFile Test.java hashmap Testjava/lang/Objectjava/lang/SystemoutLjava/io/PrintStream;java/io/PrintStreamprintln(Ljava/l ang/String;)V! *∑±
% ≤∂±
Back to the command line or terminal, type java Test.
If you get an error, which you shouldn't, I don't know what to say. It should produce the string " hashmap " on to the command line or terminal.
Why re-installing Dr. Java may not work is because you may be using the same working directory, causing same run settings to be used. Dr. Java may be running an external program, one without a main method.
I think that you should install the Eclipse IDE for Java. It is much easier to get around, it looks nicer, and it runs the file or project that you are looking at currently.
Sometimes this problem happens because may be mistake in saving file.you always your file using double quotes and with the .java extension which is main class means that class containing main method.
you should save your file by class name which is public .if there is two classes and both have main method then you should save your file by class name that is public and that class will be run.As like your compiler looking for main method in public static void main(String [] args) that is contract for jvm to run a programme
so it is not able to found that main method that is static and it looking for your Dr class.java
See this Example it have two main methods and practice these kinds of question.I also got this kind of problem in starting.
public class TestFirst
{
public static void main(String [] args){
System.out.println(" TestFirst ");
}
}
class Test{
public static void main(String [] args){
System.out.println(" hashmap ");
}
}
if you save pro-gramme by "TestFirst.java" then o/p will come TestFirst if you do some mistake in main method because we have saved our programme by TestFirst then you will get error like you got.
# 2nd mistake may be this
debian#debian:~/Geany_java$ javac Test1.java
debian#debian:~/Geany_java$ java Test1
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at Test1.main(Test1.java:11)
your classpath has not set properly See above Compiling successfully but running showing same kind of error you got.Which OS is using I can guide you properly.
Check that actually your file have the .java termination nor the .dj
There is nothing wrong with the code.
It is the executing environment which might have problem. Please share the details.
Check if program compiled correctly.
Check time-stamo of .class file.
Check permissions on folder/directory where class-files are getting generated.
Check if DrJAVA has appropriate permission on the directory.
Did you create a file, compiled it with out main?
Check class-path. Might be possible that previous class file is still being found by JDK in classpath.
Try compiling .java file from cmdLine instead of editor.
As others have mentioned, your code is fine. There must be a problem with your environment. I recently experienced a similar issue when investigating and answering this question.
Basically, in that question, the code Void.class instanceof Class resulted in a compiler error because a user-made Class.class existed in the classpath, so one Class (the Java built-in java.lang.Class) didn't match with the given Class (user-made).
Something similar may be at work here. It is possible that there is a user-made String.class in your classpath. Then in your main signature, String[] args would mean an array of your String, when Dr. Java must be looking for a main method taking an array of the Java built-in String, i.e. java.lang.String[]. If you have a custom String class in your classpath (or in your project?), then the Java compiler will choose it over the built-in String. If you were to compile and run your Test class from the command line, then you would get the runtime error: Exception in thread "main" java.lang.NoSuchMethodError: main.
Following #S0urceC0ded's suggestion, you may find this when looking at Test.class in a text editor:
main([LString;)V // A user-made String class
instead of what it's supposed to be:
main([Ljava/lang/String;)V // The built-in java.lang.String class
If so, remove your own String class (at least the .class file, but also the .java file so the .class file isn't re-created) from the classpath, and compile and run your Test class again.
Without a look at your environment, I can't tell for sure that this is the issue. But it can explain it.
If you are using Dr.Java as IDE, then you need to make sure that the main class containing 'public static void main' should be at the very top of your program. Otherwise Dr.Java throws this error during runtime.

Categories