Java compiles my program, but I cannot run it - java

I want to run a Java file with the following source code:
package u0a1;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
To run the file I did the following things:
C:\.. \u0\u0a1> javac HelloWorld.java (this works, class file is created)
Then I try to run it with:
C:\..\u0> java u0a1.HelloWorld
This step doesn't work. Main class could not be found.
I also tried
C:\..\u0\u0a1> java HelloWorld
C:\..\u0> java u0a1\HelloWorld
None of them worked.

This is a piece i found somewhere else, worked for me.
Have you set your JAVA_HOME correctly? If not you have to work with
the full path
Example: "C:\Program Files\Java\jdk1.7.0_51\bin\javac.exe"
HelloWorld.java
If you have runtime issues, you should work it out like this
Select MAIN directory - not package directory
java u0a1/HelloWorld
If you have problems with CLASSPATH or JAVA_HOME - try this:
"C:\Program Files\Java\jdk1.7.0_51\bin\javac.exe" HelloWorld.java
source: http://quandano.com/questions/how-to-run-a-java-file-within-a-package-from-cmd

You are compiling a program of package , so it should compile this way
C:.. \u0\u0a1> javac -d . HelloWorld.java
here -d for creating package u0a1
and "."
for from current working directory
after compiling this way a folder will create with name "u0a1"
then other thing will work properly

Related

Running this from command line in Java

I cannot run the following program from command line the usual way:
package animal_package;
public class my_animal {
public static void main(String[] args) {
System.out.println("Hello animal");
}
}
E.g. from Command prompt: I go to "D:\Java\src\animal_package" where my java program is and compile it:
D:\Java\src\animal_package>javac my_animal.java
D:\Java\src\animal_package>java my_animal
Error: Could not find or load main class my_animal.java
I have looked on Google and came around class path problem but couldn't make any sense from it all.
Which command line would be correct in my case?
To compile:
javac my_animal.java
To run (from src directory):
java animal_package.my_animal
cd .. then java -cp . animal_package.my_animal. The package is part of the fully qualified class name. Or,
java -cp .. animal_package.my_animal
You can compile the class either from src directory using:
javac animal_package\my_animal.java
OR from animal_package directory using: javac my_animal.java
To run the program from src directory use
java animal_package.my_animal OR java -cp . animal_package.my_animal

Getting error "cannot find or load main class HelloWorld"

I have this simple code:
public class HelloWorld{
public static void main(String[] args){
System.out.println("HelloWorld");
}
}
And the filename HelloWorld.java
In command prompt, I type in:
javac HelloWorld.java
java HelloWorld
(same directory)
I am getting the error: "cannot find or load main class HelloWorld"
I'm sure it has nothing to do with improper installation because I reinstalled jdk and jre twice.
Edit:
This was working before, and the next day, no change of code, directory, or anything, its started giving an error.
You could get this behaviour if you have an incorrect / inappropriate setting of the CLASSPATH environment variable; e.g. the current directory isn't on the classpath. (It is by default ... )
Try this:
java -classpath . HelloWorld
Assuming that works ... the problem is your understanding of the concept of the "classpath". This is explained well by the Oracle documentation:
The java & javac command documentation ... particular the -classpath argument
Setting the Classpath.
The Java Tutorial - PATH and CLASSPATH
try:
java -cp "C:\WhatEverDirectoryYourFileIsIn" HelloWorld
In CMD, instead of typing:
java HelloWorld
Try typing:
java HelloWorld.class

How to access packages?

I wrote a program HelloWorld.java
and stored in a folder(package) named test that test includes hello folder by itself.
and all in my workspace.
I mean this way: d:\workspace\test\hello\HellWorld.java
And I entered the d:\workspace in my path environment, my code:
package test.hello;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("HelloAll");
}
}
When I go to the hello directory at CMD and compile HelloWorld.java everything is fine and done.
but as I use java HelloWorld (in d:\workspace\test\hello) I get exception in thread main error.
Can you help me with this just simple question?
You must use the fully qualified name of your class to run it.
Stand in d:\workspace\
Run:
java test.hello.HelloWorld
in cmd windows go d:\workspace and issue the following cmd
d:>workspace>java test.hello.HelloWorld
cd to d:\workspace
Compile using-
javac -d . HelloWorld.java
The above will create package structure.
Run using-
java test.hello.HelloWorld
You need to use java command from your source directory i.e. d:\workspace as mentioned here:
java test.hello.HelloWorld
The syntax is simple, just go to your source code directory and not the package directory. Use the classname along with full package name.

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.

Running java class from terminal

This question has been asked before but I still cannot figure whats wrong for some reason.
I got a class named NewClass in package syntaxtest in file src. From src path I type :
javac src/syntaxtest/NewClass.java
and the class is compiled and I can see NewClass.class in syntaxtest folder. Now from that same path or even the same folder with NewClass.class, I can't figure out how to run the class from terminal. I have made many different attempts but ether I get
ClassDefNotFound or ClassDefNotFound (wrong name : syntaxtest/NewClass)
Try "java -cp src syntaxtest.NewClass".
That is, if you have a folder "src" which contains the subfolder (package) "syntaxtest" and the class "NewClass" is in "package syntaxtest", then the above command will work.
$ ls src/syntaxtest
NewClass.java
$ cat src/syntaxtest/NewClass.java
package syntaxtest;
public class NewClass {
public static void main(String args[]) {
System.out.println("Hello, World!");
}
}
$ javac src/syntaxtest/NewClass.java
$ java -cp src syntaxtest.NewClass
Hello, World!
I've made the following test:
Created a java file in home/test/blah/TestClass.java
package blah;
public class TestClass {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Went to directory home/test/
Compiled the file by typing:
javac blah/TestClass.java
Got the file compiled ok.
Ran it by typing:
java blah.TestClass
Got the message "Hello World!" as expected: program runs ok.
Went to directory home/
Tried to run by typing:
java test/blah.TestClass
... and many other combinations of slashes and dots..... nothing worked.... keep getting the same Exception as you:
java.lang.NoClassDefFoundError
So it seems to me that to run a Java class using the command 'java' you really must be in the application's root folder.
I had a similar problem.
I wanted to organize my project using a src and bin folders directly from the Mac finder and use Emacs or some other text editor. I just didn't like eclipse.
You can't execute the classes from another folder, but you can do is compile from another folder into the one you are going to execute.
For example (assuming there is no package), move to the bin folder and run:
$ javac ../src/name.java -d ../bin/
(That compiles from the src folder and outs the .class file directly on bin)

Categories

Resources