Running this from command line in Java - 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

Related

Could Not Load Main Class (Java)

Probably this is a repeated question, but I just couldn't find an answer to what I am looking for! I am trying to compile and run a java class in a Unix box.
I have the class as:
package tmp.test;
import org.jasypt.registry.AlgorithmRegistry;
class Algo {
public static void main(String[] args) {
System.out.println(AlgorithmRegistry.getAllPBEAlgorithms());
}
}
The files are in the path /tmp/test/. Now I compile the class with the command:
javac -cp jasypt-1.9.3.jar Algo.java
The JAR file is in the same directory. It compiles just fine. But when I run the class file with the command:
java -cp jasypt-1.9.3.jar Algo
I get the error:
Error: Could not find or load main class Algo
I am executing all the commands from the path /tmp/test/.
I tried:
java -cp jasypt-1.9.3.jar tmp.test.Algo
and
java -cp jasypt-1.9.3.jar tmp/test/Algo
Both throw the same error.
I am not sure what I am doing wrong. At first I thought it was the problem of the access thing. So I changed everything using chmod to 777. Everything seems to be fine. Can you please let me know what I am missing here?
I am executing all the commands from the path /tmp/test/
That is the problem. You need to be one level above tmp, not somewhere inside. Then your command line
java -cp /path/to/jasypt-1.9.3.jar tmp.test.Algo
should work. If you insist in starting Java from the subdirectory inside your classpath, you can do this quite contrived thing:
java -cp /path/to/jasypt-1.9.3.jar:../.. tmp.test.Algo
tl;dr
Use the switch -d to compile and then use the fully qualified name of the class to run it.
Compile the class as follows:
javac -d . -cp jasypt-1.9.3.jar Algo.java
The switch, -d specifies where to place generated class files and . stands for the current directory.
Run the class as follows:
java -cp jasypt-1.9.3.jar tmp.test.Algo

How can I call the method of class in a jar through terminal?

I have a jar file which includes classes. I need to call the method of a class which is in the jar file through the terminal. Can anyone help me?
You can only call the classical main method of a class from the terminal, given that it is defined in the JAR's manifest.
public static void main(String [ ] args) {
....
}
I assume you want to know how to run code that uses a class/method from a jar, in terminal.
If your code (HelloWorld.java) uses a method from your.jar, this is how you can compile/run your code and call that method from within HelloWorld.java:
On Linux :
javac -classpath your.jar:. HelloWorld.java
java -cp your.jar:. HelloWorld
On windows :
javac -classpath your.jar;. HelloWorld.java
java -cp your.jar;. HelloWorld

Java compiles my program, but I cannot run it

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

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.

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.

Categories

Resources