Issues with running a compiled java program - java

I am a Java beginner. I wrote a quintessential "Hello, World!" program. It compiles, but won't run. The terminal says there is an exception in the thread main, and that the class hello is not found. I am using Ubuntu 12.04. What could be wrong here?
The file is called hello.java.
The commands I used:
$javac hello.java
$java hello
My code is below:
class hello{
public static void main(String[] args) {
system.out.print("Hello");
}
}
EDIT-----------------------------------------------------------------------------------------
I just realized that I am using openjdk7. Does that pose a problem?

class must be: public class hello
system.out.print is wrong, must be: System.out.println("Hello World");

Is the filename hello or Hello? The only way I duplicated your problem was by having the class name wrong, and in java the class name an filename must exactly match (meaning the case too). So if your filename is Hello.java and the class name is hello the program will compile fine but throw the same error you mentioned. This is just a guess though.

Is that all your code? Since you use a terminal, can you add the results of the command ls -lR to your question? (run it in the same directory as your original command executing the application).

Related

Java program runs yet compilation fails

I wrote a Java program whose filename was (intentionally) different from the class I wrote inside the file. The javac command failed as expected on both CMD and WSL. The java command however worked and ran my print statement. I wrote the code intentionally this way so there is no way it was a previously compiled version of the code. The following code was written in a file called "explainJava.java" (notice the filename is different from the class name).
public class explain{
public static void main(String[] args) {
System.out.println("Java is weird");
}
}
I've had to google this myself, but I think I've found an explanation in this article.
According to that source as of Java 11 java is capable of compiling a single source file into memory.
What I conclude from that: When the file is compiled into memory and not written to disk it obviously cannot have a file name. If there is no filename there is no such thing as a wrong filename, therefore the code executes.
Please also note that the restriction of having to name a file like the public class within that file is more of a design decision to make work for the compiler easier/ faster. It is not a physical restriction so to speak. Have a look at the following thread for more details.
If you put this code:
public class explain {
public static void main(String[] args) {
System.out.println("Java is weird");
}
}
into a file named explainJava.java, and then compile it with this:
javac explainJava.java
you will get an error that correctly informs you that your filename ("explainJava") and the class defined inside that file ("explain") do not match:
explainJava.java:1: error: class explain is public, should be declared in a file named explain.java
public class explain{
^
1 error
If you run this command:
$ java explainJava.java
Java is weird
you see expected output, because you're skipping the explicit compilation step (that is, you aren't running javac first) and instead relying on behavior introduced in Java 11 that allows you to compile+run in a single step. Here's an explanation: Does the 'java' command compile Java programs?
So the answer is to either:
rename your file to match the class, so change the filename to "explain.java", or
rename the class to match the file, change public class explain to be public class explainJava

New to Java , error already

package day1.examples;
public class String2 {
public static void main(String[] args) {
String x = "Andrei Vlad";
System.out.println("Hello" + x);
}
}
I keep getting this error when i run it
Error: Main method not found in class day1.examples.String2, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
and on line error it says type mysmatch cannot convert from java lang string etc..
Thank you
It works for me:
[steve#newbox ~]$ cd /tmp=
[steve#newbox tmp]$ mkdir -p day1/examples
[steve#newbox tmp]$ cat > day1/examples/String2.java
package day1.examples;
public class String2 {
public static void main(String[] args) {
String x = "Andrei Vlad";
System.out.println("Hello" + x);
}
}
[steve#newbox tmp]$ javac -classpath . day1/examples/String2.java
[steve#newbox tmp]$ java -classpath . day1.examples.String2
HelloAndrei Vlad
[steve#newbox tmp]$
The most likely explanation is that you have managed to get Eclipse rather confused.
My initial idea was that this was a homoglyph problem. But provided that you have copy-and-pasted the code correctly, the evidence disproves that.
The other idea I had was that you had mistakenly created your own version of the String class (in the day1.examples package). However, that should have resulted in compilation errors in the initialization of x.
I'd start by trying with a clean of the project. Seems weird you would need to do so with only one class in a single package but it will recompile all of your code for you. Everytime I have an issue that I don't think I should have I always F5 (refresh) my packages and then clean. You'd be surprised how many issues it fixes.
Go to Project --> Clean
Then try running again.
The code says that there are somethink wrong with your main class but all seems ok.
In Eclipse rigth click on your projects-> run-> run configurations and check if all is ok.

Run Java Program in Iseries(AS400)

Using Qshell,I have compiled Java Program that is saved in IFS Folder.After Compilation,Class file has been generated.But when i tried to run this class,it is not displaying any output.It simply gives $Prompt without any error.But when i checked the spool files,it is showing this:"Unable to Complete Java Program because of reason 04 and Code 4 means:Unable to find method id required to run java program.."
Ex. Simple Hello World Program :System.out.prinln("Hello World");
Compile:-cd /test(where test=directory where program is saved)
javac sample.java
Run:-java cp /test sample
The main method signature may not be valid. The code should be placed inside a properly defined main method:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
To compile and execute from QSH:
$ javac HelloWorld.java
$ java HelloWorld
Hello World
See Lesson: A Closer Look at the "Hello World!" Application for more information.

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.

Error: Could not find or load main class Hello2

I'm just learning java and following a book.
I have a program written via text editor and run commands via cmd.
I've complied 1 program thru javac and executed thru java no problem. (Hello)
Then I modified that to add a comment to the class, named file Hello2.java. I compiled it with no problems, but upon execution, I receive this error: Could not find or load main class Hello2.
I have classpath and path set correct;y on environment variables.
Ideas?
UPDATE
Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Hello2.java
//Filename Hello2.java
//Written by
//Written on
public class Hello2 {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
/*This class demonstrates the use of the println() method to print the message Hello, world! */
I've found the solution to my problem. I know it's not a code problem. But what I did is that I deleted CLASSPATH from system variables and everything now works...at least for now.
Thanks a lot everyone for your inputs, much appreciated!
You have to change the name of the public class too when you change the name of the file. So, if your file is called Hello2.java, the class should be called Hello2 and not Hello.
Are you sure you set the classpath correctly? Why don't you try running java -cp the directory of the .class file Hello ? If that doesn't work please upload the full stacktrace.
You must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath.
Note that the windows classpath separator is a semi-colon ie ;
If your class file is saved in following directory with Hello2 program name
d:\sample
java -cp d:\sample Hello2
java -cp . Hello2
I believe you have Hello2.java file as below.
class Hello {
public static void main (String args[]) {
System.out.println("Hello");
}
}
Change it to
class Hello2 {
public static void main (String args[]) {
System.out.println("Hello");
}
}
The change is class Hello2 instead of class Hello.
Note : You should always have classname and file name SAME.
Good Luck!!!
Update 1
Are you doing below steps??
Wrote Hello.java
Compile by javac Hello.java
Run by java Hello
Rename Hello.java to Hello2.java
Rename class name i.e. class Hello to class Hello2
javac Hello2.java
java Hello2
I believe you are missing Step 6 & executing step 7 after step 5. Please confirm.

Categories