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.
Related
When I try to run a java program in the cmd it displays the messege "Could not find or load main class".
The problem occurs when there is a package involved in the program, otherwise it works fine.
The code is:
package myPackage;
public class index {
public static void main(String [] args){
System.out.println("Hello World");
}
}
I've tried writting in cmd: javac (name of package) . Class name, but still doesnt work.
The issue was that the class path needs to be set for each command (javac and java):
Attempted Steps
Compile index.java from the top_level. do not use sub package.
$javac -cp . importpackage/subpackage/index.java
I have:
public class JavaBatchInserter {
public static void main(String []args) {
System.out.println("Hello World");
}
}
I go to the pwd where my file, named JavaBatchInserter.java, is located and run:
java JavaBatchInserter.java
and get:
Could not find or load main class JavaBatchInserter.java
It's the simplest thing and I can't make it work. ): Help please! Thanks!
Note: I don't want to create a project and a bunch of directories if possible.
Firstly, compile the file JavaBatchInserter.java
javac JavaBatchInserter.java
Then give your full class name to JVM for execution
java JavaBatchInserter
You run the command java JavaBatchInserter.java
It is wrong.First compile the class by running javac JavaBatchInserter.java
The try to run by java JavaBatchInserter
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).
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
package in .java file makes class file unuseable
My Hellow World runs fine.
But as soon as I add a package reference I cannot run it from the command line:
package pv;
public class hcw2 {
public static void main(String[] args) {
System.out.println("Hello Cruel World.");
}
}
compiles fine, then I expect to use java pv.hcw2 to run it, as:
>java pv.hcw2
>Error: Could not find or load main class pv.hcw2
I have also tried just java hcw2, to no avail.
Running in the same directory as the original which runs. Running on Windows 7 64b.
Thank you
You should have a folder called pv under which your file hcw2.java should lie. The folder pv is nothing but your package. Then outside the directory you may issue a javac command as shown below followed by java.
braga#braga-laptop:~$ javac pv/hcw2.java
braga#braga-laptop:~$ ls pv
hcw2.class hcw2.java
braga#braga-laptop:~$ java pv.hcw2
Hello Cruel World.
You have to keep your class in the folder named your package. so for :
package pv;
public class hcw2 {
public static void main(String[] args) {
System.out.println("Hello Cruel World.");
}
}
The hcw2.java should be kept like pv\hcw2.java
once you compile successfully there should be the class file in same folder like :
pv\hcw2.class
While running you have to change directory to the base directory. so if your directory structure like : d:\java\pv\hcw2.java
then
Change dir to d:\java>
Run the java command there with the package name. So :
d:\java> java pv/hcw2 or
D:\java>java test.Test
Your java class needs to be inside a subdirectory called pv. So:
mkdir pv
mv hcw2.java hcw2.class pv
then you can run
java pv.hcw2