Running a Java Class in Ubuntu with Virtual Box - java

Since I'm quite new in Java i have a question. As told on the title , I run an Ubuntu Server on VirtualBox and I have a problem running a very simple class with the use of package.
I give you the code:
package world;
public class HelloWorld{
public static void main (String[] args){
System.out.println("Hello World")
}
}
Very simple code indeed. After compiling it with javac HelloWorld.java, with no mistakes (ok now what mistakes could possible find),
Running java HelloWorld, gives me the message NoClassDefFoundError
Running java world.HelloWorld returns cannot find or load main class.
I suspect that it has something to do with classpath , but I cannot find an answer.

It's a classpath issue. You can probably check to see what your classpath is by looking at the CLASSPATH environment variable. You can try adding the directory your classfiles are at to the end of this CLASSPATH, but the simplest thing to do is probably the following.
Make sure the HelloWorld.java file is in a directory called world, and you can compile is like:
javac world/HelloWorld.java
This will create a HelloWorld.class file in the world directory. You can then try running
java world.HelloWorld
or
java -classpath . world.HelloWorld
from the same place.
You can also use the -d flag with javac to put the class files in a different place instead of the same place the source (.java files) are.

Related

"Could not find or load main class" Error while running java program using cmd prompt [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 2 years ago.
I am running a simple "HelloWorld" Program. I get this error in the command prompt:
Could not find or load main class HelloWorld.
I have set the CLASSPATH and PATH variable in the system. In the cmd prompt, I am running from the directory where I have saved HelloWorld program. I can see the class name and the file name are same and also .class file created in the same directory. What else could be the problem?
My sample program looks like this:
package org.tij.exercises;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World!!");
}
}
When the Main class is inside a package then you need to run it as follows :
java <packageName>.<MainClassName>
In your case you should run the program as follows :
java org.tij.exercises.HelloWorld
What's your CLASSPATH value?
It may look like this:
.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
I guess your value does not contain this .;.
So, ADD IT .
When you done , restart CMD
That may works.
For example the file HelloWorld.java is in path: D:\myjavatest\org\yz\test and its package is: org.yz.test.
Now, you're in path D:\myjavatest\ on the CMD line.
Type this to compile it:
javac org/yz/test/HelloWorld.java
Then, type this to run it:
java org.yz.test.HelloWorld
You may get what you want.
I removed bin from the CLASSPATH. I found out that I was executing the java command from the directory where the HelloWorld.java is located, i.e.:
C:\Users\xyz\Documents\Java\javastudy\src\org\tij\exercises>java HelloWorld
So I moved back to the main directory and executed:
java org.tij.exercises.HelloWorld
and it worked, i.e.:
C:\Users\xyz\Documents\Java\javastudy\src>java org.tij.exercises.HelloWorld
Hello World!!
Since you're running it from command prompt, you need to make sure your classpath is correct. If you set it already, you need to restart your terminal to re-load your system variables.
If -classpath and -cp are not used and CLASSPATH is not set, the current directory is used (.), however when running .class files, you need to be in the folder which consist Java package name folders.
So having the .class file in ./target/classes/com/foo/app/App.class, you've the following possibilities:
java -cp target/classes com.foo.app.App
CLASSPATH=target/classes java com.foo.app.App
cd target/classes && java com.foo.app.App
You can check your classpath, by printing CLASSPATH variable:
Linux: echo $CLASSPATH
Windows: echo %CLASSPATH%
which has entries separated by :.
See also: How do I run Java .class files?
I had the same problem, mine was a little different though I did not have a package name. My problem was the Class Path for example:
C:\Java Example>java -cp . HelloWorld
The -cp option for Java and from what I can tell from my experience (not much) but I encountered the error about 20 times trying different methods and until I declared the class Path I was receiving the same error. Vishrant was correct in stating that . represents current directory.
If you need more information about the java options enter java -? or java -help I think the options are not optional.
I just did some more research I found a website that goes into detail about CLASSPATH. The CLASSPATH must be set as an environment variable; to the current directory <.>. You can set it from the command line in windows:
// Set CLASSPATH to the current directory '.'
prompt> set CLASSPATH=.
When you add a new environment setting you need to reboot before enabling the variable. But from the command prompt you can set it. It also can be set like I mentioned at the beginning. For more info, and if your using a different OS, check: Environment Variables.
One reason for this error might be
Could not find or load main class <class name>
Maybe you use your class name as different name and save the class name with another name you can save a java source file name by another name than class name. For example:
class A{
public static void main(String args[]) {
System.out.println("Hello world");
}
}
you can save as Hello.java but,
To Compile : javac Hello.java
This will auto generate A.class file at same location.
Now To Run : java A
Execute your Java program using java -d . HelloWorld command.
This command works when you have declared package.
. represent current directory/.
I had a similar problem when running java on win10
instead of
$ java ./hello
Error: Could not find or load main class ..hello
Run
$ java hello
Hello, World
I was getting the exact same error for forgetting to remove the .class extension when running the JAVA class. So instead of this:
java myClass.class
One should do this:
java myClass
I used IntelliJ to create my .jar, which included some unpacked jars from my libraries. One of these other jars had some signed stuff in the MANIFEST which prevented the .jar from being loaded. No warnings, or anything, just didn't work. Could not find or load main class
Removing the unpacked jar which contained the manifest fixed it.
I faced the same problem and tried everything mentioned here.
The thing was I didn't refresh my project in eclipse after class creation .
And once I refreshed it things worked as expected.
faced the same problem. solved by following these steps
go to directory containing the package 'org.tij.exercises' (e.g: in eclipse it may be your src folder)
use java org.tij.exercises.HelloWorld
For a lot of us, at least for me, I think the class path hierarchy is not intuitive since I'm working inside a directory structure and it feels like that ought to be it.
Java is looking at the name of the class based on it's package path, not just the file path.
It doesn't matter if:
i'm in the local directory ./packagefoo/MainClass, or
a directory up ./packagefoo/, or
one down ./packagefoo/MainClass/foo.
The command "java packagefoo.MainClass" is running off the root %CLASSPATH% which means something significant to Java. Then from there it traverses package names, not path names like us lay coders would expect.
So if my CLASSPATH is set to %CWD%/, then "java packagefoo.MainClass" will work.
If I set the CLASSPATH to %CWD%/packagefoo/ then packagefoo.MainClass can't be found.
Always "java MainClass" means nothing, if it is a member of "package", until I rip out the java code "package packagefoo;" and move the Class File up a directory.
In fact if I change "package packagefoo;" to "package foopackage;" I have to create a subfolder under CLASSPATH/foopackage or foopackage.MainClass stops working again.
To make matters worse, Between PATH, CLASSPATH, JAVAHOME, for Windows, JDeveloper, Oracle Database, and every user name it was installed under, I think a coder trying to just get something up fast ends up brute forcing path variables and structure until something works without understanding what it means.
at least i did.
Create a folder org/tij/exercises and then move HelloWorld.java file. Then run below command
javac -cp . org/tij/exercises/HelloWorld.java
AND
java -cp . org/tij/exercises/HelloWorld
I was facing similar issue but it was due to space character in my file directory where I kept my java class.
Scenario given below along with solution:
public class Sample{
public static void main(String[] args) {
System.out.println("Hello world, Java");
}
}
My Sample.java class was kept at Dir "D:\Java Programs\Sample.java"[NOTE: Package statement not present in java class].
In command prompt, changed directory to "D:\Java Programs\", my programmed compiled but failed to run with error "Could not find or load main class"
After all the possible solutions over SOF(nothing worked), I realized may b space causing me this issue.
Surprisingly removal of folder name space char['Java Programs' -> 'JavaPrograms'], my program executed successfully. Hope it helps

How to run a simple java class on the command prompt in windows 8

I'm having a bit of trouble trying to run a java class on the command prompt. Its a very simple class and a friend of mine says it could be a problem with windows 8. Are there any suggestions. Here i'll show you the class and how I tried to compile it. I works fine in eclipse.
package gmit;
public class A {
public static void main(String[] args) {
System.out.println("hello");
}
}
In the command prompt I wrote
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>
followed by - javac A.java
java A.class
I tested the class using type A.java
and the text from the class does come up.
What am I doing wrong? Any help would be great.
If it runs in an Eclipse project, but not from command line, you could try this:
C:\Users\eclipse\workspace\Oct1stcasting\bin\gmit>java A
Inside a project directory, Eclipse saves source codes in /src and bytecodes in /bin. Thus, if you simply want to run your bytecode from command line, changing directory to /bin might suffice.
To compile
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>javac A.java
To run
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>java A
Don't append the .class extension while running the java program
Don’t add a .class suffix when using the java command.
Use java A or java -cp . A. The latter is required if the current directory is not implicitly used as class path.
Compilation:
C:\Users\eclipse\workspace\Oct1stcasting\src>javac gmit/A.java
Executing:
C:\Users\eclipse\workspace\Oct1stcasting\src>java gmit/A

NoClassDefFoundError error while running JAVA console app

I created little HelloWorld example and I am having problem running it from command prompt (on Windows). When I attempt to run it by:
java tcpServer from command prompt I get NoClassDefFoundError
I am able to compile it with javac, and class file gets generated.
Somewhere I was reading that I have to put path to my class folder into CLASSPATH environment variable. I've done it and rebooted machine, but I still get the same error.
I also was trying to run it by java -cp c:\MyFolderWhereClassFileIs HelloWorld, it doesn't work.
I've looked into ENV variables and I have following:
JAVA_HOME: C:\Program Files (x86)\Java\jdk1.6.0_26;
JRE_HOME:C:\Program Files (x86)\Java\jre6;
CLASSPATH: C:\HelloWorld;
So, how do I run this?
Any ideas how to resolve this? Thanks.
PS. The most annoying thing to me is, if I create java project in eclipse, and create HelloWorld example, then it runs fine...
UPDATE:
Here is the code. It does have package specified.
package test.com;
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello World");
}
}
My HelloWorld.java and HelloWorld.class files are here:
C:\workspace\TestApp\src\test\com
One thing I learned so far is that I can't run it from within com folder or test folder. I have to be in src folder class file can be found... but I am still not able to run it... always the same error.
Does your class have a package name? That is, a statement at the top which reads
package <mypkgname>;?
If so, then you have to (A) create the correct directory structure and (B) prefix the class name with the package name in your java command.
For example, if your Java file looks like this:
package hello;
public class HelloWorld {
...
}
Then a basic directory structure would resemble:
src/hello/
src/hello/HelloWorld.java
src/hello/HelloWorld.class
And your bash command would be:
cd src
java hello.HelloWorld
Otherwise, if your class has no package name, it will be placed in the default package. In this case, you simply have to cd to the directory where your class file resides, and run java HelloWorld.

NoClassDeffFoundError when running java.exe from different directory than .class files

I'm trying to learn to run java apps from windows command line and I can't figure out one problem.
I have a simple class on my desktop:
public class Hello{
public static void main(String[] args){
System.out.println("1, two, three");
}
}
If I run javac and java commands when I'm in my desktop directory in cmd everything is well, but if I go one directory back (so I won't be in the same directory as the .java and .class files) then my cmd directory is C:\Users\Tomas and my Hello.java and Hello.class files are in C:\Users\Tomas\Desktop. I can run the command javac Desktop\Hello.java and it works, but then if I try to do java Desktop\Hello.java I get an Exception in thread "main" java.lang.NoClassDefFoundError: Desktop\Hello (wrong name: Hello).
I know that NoClassDefFoundError is throws when a class was available at compile time, but ClassLoader can't find it during run time ( found a good article about it here).
I think the problem has something to do with the CLASSPATH variable, so I set it to:
"C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext";.;"C:\Program Files\Java\jdk1.7.0_21\jre\bin";"C:\Users\Tomas\Desktop"
(I included "C:\Users\Tomas\Desktop" just to try everything)
And I tried running the "java" command with -classpath and -cp options:
java -classpath "C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext";.;"C:\Program Files\Java\jdk1.7.0_21\jre\bin";"C:\Users\Tomas\Desktop" Desktop\Hello
And I keep getting the same exception.
I't would be great if someone can explain my error and why this is happening, and maybe point even give some directions where can I read more about this.
Thank you.
class
package Desktop;
public class Hello{
public static void main(String[] args){
System.out.println("1, two, three");
}
}
compile (here Desktop means standart windows directory)
javac Desktop\Hello.java
execute (here Desktop means package. Desktop/Hello is fully class name)
java Desktop/Hello
java -classpath 'C:\Users\Tomas\Desktop\Hello.class'
Should run it.
Try java -classpath "C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext";.;"C:\Program Files\Java\jdk1.7.0_21\jre\bin";"C:\Users\Tomas\Desktop" Hello
I only removed Desktop from your class name.

How to execute my HelloWorld script

I'm currently trying to run my first java script:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I decided i'd take a little look into Java. However I come from languages like JavaScript and PHP which don't require any compiling or anything as such.
So far, I think i'm compiling it correctly in the command prompt:
C:\Users\Shawn>"C:\Program Files\Java\jdk1.7.0_25\bin\javac.exe" "HelloWorld.java"
It adds a file called: HelloWorld.class so figured I did something right.
However, now when I try to actually run the program using:
C:\Users\Shawn>"C:\Program Files\Java\jdk1.7.0_25\bin\java.exe" "C:\Users\Shawn\HelloWorld.class"
I get this, Error: Could not find or load main class C:\Users\Shawn\HelloWorld.class.
However, if I try that same command but use javac.exe instead I get:
javac: invalid flag: C:\Users\Shawn\HelloWorld.class
Usage: javac <options> <source files>
use -help for a list of possible options
Why is this happening? Why isn't my program executing correctly?
The java command takes the name of the class, not the name of the file.
It then uses the Java class loader to find the .class file for that class in the current directory or the class path.
When you pass HelloWorld.class, it looks for a class named class in the package HelloWorld.
(that would be ./HelloWorld/class.class)
You need to pass HelloWorld.
As others have pointed out the argument needs to be just the class name, without the extension .class. There is a second requirement that must be met, though: the class file must be in the classpath.
It's usually not advisable (although convenient) to include the current directory in the global classpath, but you can override it on the command line:
java -cp . HelloWorld
You can also specify an explicit path:
java -cp "C:\Users\Shawn" HelloWorld
If your Java program uses other classes, include the global classpath like this:
java -cp "%CLASSPATH%;." HelloWorld
See the following javac documentation for more info. especially the section on Cross-Compilation Options.

Categories

Resources