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
Related
I ahve tried several approaches as on How do I run a Java program from the command line on Windows? and create exactly the same class and packages to use the same things.
Here is the sample class located on C:\SimpleJavaProject\src\com\hello\programs:
package com.hello.programs;
public class ABC {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Then I compile it in the usual way:
C:\SimpleJavaProject\src\com\hello\programs > javac ABC.java
Later, run it by giving the package name and then my java class name:
C:\SimpleJavaProject\src > java com.hello.programs.ABC
In each time I try to run java app, I get "Error: Could not find or load main class com.hello.programs.ABC" error. Then have a look at What does “Could not find or load main class” mean? page and tried some approaches on that page. But still the same error.
It is too simple, but still I have not managed to run the simple app yet. So, how to fix this problem? And after running the app, how can I pass args on cmd?
Update: I could already generate ABC.class file by running the following command. BUT, I cannot run the app and see the "Hello world" on the console.
cd C:\SimpleJavaProject\src\com\hello\programs
javac ABC.java
--> generates ABC.class in C:\SimpleJavaProject\src\com\hello\programs
java com.hello.programs.ABC
When you run this, java is going to check each and every CLASSPATH path for that + /com/hello/programs/ABC.class, will load that, and then run it.
This must mean that either:
[A] Your classpath does not include the current directory; The fix is java -cp . com.hello.programs.ABC.
[B] you didn't do what you wrote, and e.g. dir com/hello/programs.ABC.class prints nothing.
Note that you're not doing it right; a class file should never be in a directory path that includes src. If you don't want to bother with build tools like maven or gradle, I strongly suggest you don't bother with a src dir then either. If you must, the -d option can be passed to javac to tell it where tou put the file. If you want to separate source and class files, then that should be targeting a directory named bin or build or whatnot (a sibling of the src dir).
When you use javac ABC.java you are compiling the class, and javac places it in the current directory.
So java com.hello.programs.ABC would not work (because com/hello/programs/ABC.class file does not exists).
You can use the javac -d flag:
-d <directory> Specify where to place generated class files
For instance:
> javac -d . ABC.java
> java com.hello.programs.ABC
Hello world
> cd com\hello\programs
> dir
ABC.class
Would work, because javac did place ABC in com/hello/programs.
Update: For clarity, once you compiled using javac -d . ABC.java you can run it using java com.hello.programs.ABC and you should see Hello world in the screen.
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
I exported a jar file from eclipse and in the jar file are various packages and sub packages with java class files.
I'd like to run one of these nested class files on the command line in Windows.
The class has a main and I am using the following to try to run it,
java -classpath .;./example.jar example
Note that example is the name of the class as well as the jar.
I've also tried to spell out the full path of the class
java -classpath .;./example.jar the.whole.path.example
How can I run the example class?
EDIT:
OK this is kind of stupid, the full path was incorrect. I checked this over numerous times yet it was only when I came back to it that I noticed the error.
Just running java -cp example.jar the.whole.path.example should do the trick. If not, then something with your JAR file is wrong. The class name must be fully qualified (with package name) and the specified class must have a correct main method:
public static void main(String[] args) {
}
Without seeing any of your code or any output (hint, hint) it's hard to say what's happening, but this works for me:
$ cat x/y/z/A.java
package x.y.z;
public class A
{
public static void main(String args[]) throws Exception
{
System.out.println("here in A");
}
}
$ javac x/y/z/A.java
$ jar cvf a.jar x/y/z/A.class
added manifest
adding: x/y/z/A.class(in = 459) (out= 311)(deflated 32%)
$ java -classpath a.jar x.y.z.A
here in A
And in case the poster or someone reading this in the future isn't familiar with Unix, the lines starting with $ are commands I type into the shell and everything else is output from those commands. Eclipse will take care of the first three for you, then the final java -classpath a.jar x.y.z.A is the command to execute the main method in the x.y.z.A class.
Check if your assumptions are correct. Show the structure of your jar and compare it to what you try to run:
jar tf example.jar
If the jar contains a lot of entries you might want to grep / find the relevant class:
jar tf example.jar | find /i "Example"
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.
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.