I'm trying to use groovyc, but something is not right:
>echo println("Hello world") > test.groovy
>groovy test.groovy
Hello world
>groovyc test.groovy
>java -cp C:\utils\groovy-1.8.1\embeddable\groovy-all-1.8.1.jar test
Error: Could not find or load main class test
>dir test.class
...
11/10/2011 02:54 PM 7,104 test.class
What am I missing?
When you specify the classpath with -cp switch, its default value (current directory) is overwritten and so JVM can't find your class.
Add current directory to classpath, and everything works:
>java -cp C:\utils\groovy-1.8.1\embeddable\groovy-all-1.8.1.jar;. test
Hello, world
Make sure that if you are using a unix based system (Linux or Mac), then you need colon instead of semicolon for classpath entry separator:
>java -cp /path/to/groovy/embeddable/groovy-all-1.8.1.jar:. test
Hello, world
I am not sure these snippets will work, since class with main method is missed.
Proper command line is:
java -cp /path/to/groovy/embeddable/groovy-all-1.8.1.jar groovy.lang.GroovyShell test.groovy
Related
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
I am executing 2 java classes which have StringUtils function. For this I have common-lang3.jar so I am giving command
`javac -cp common-lang3.jar *.java`
this time no error but after that
java -cp common-lang3.jar abc.class its giving
Could not find or load main class For linux server where I have to place jar files so that i can just give command javac *.java ? If in classpath java -classpath is not allowing me to edit. pls help
Change
java -cp common-lang3.jar abc.class
to (assuming abc has your main method)
java -cp common-lang3.jar:. abc
I am very new to groovy and I am trying out this example from the Groovy in Action book. I have this fibonacci.groovy program and when trying to run the program with java command, I am getting the NoClassDefFound error.
The command I am using in the console is:
java -cp %GROOVY_HOME%/embeddable/groovy-all-2.2.0.jar;classes fibonacci
As you can see, I have mentioned the groovy-all jar in the classpath and I set the GROOVY_HOME variable. The classpath variable is not set, so I am assuming it has the default '.' value to find in the current folder itself. What am I doing wrong?
Aren't you missing the current folder in the classpath?
I'm on Linux, but if i compile a Groovy class with groovyc and then try to run it with java, i need to tell java where is my groovy-all.jar and also add the current dir to the classpath
So, this compilation works:
$ groovyc Fib.groovy
But this run doesn't runs:
$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:classes Fib
As it's missing the current dir in the path:
$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:. Fib
test for fib
Also note that if fibonacci is in a package, you need to type the full path to the class. So for this groovy source:
package up.foo
println "test for fib"
Compile:
$ groovyc Fib.groovy
We write the full package path to execute:
$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:. up.foo.Fib
There it is:
test for fib
I've got the following structure
lib/junit-4.10.jar
tests/Tester.java
tests/Tester.class
build/jar/jar_file.jar
(Tester belongs to package tests)
I can compile tests using
javac -cp build/jar/jar_file.jar:lib/junit-4.10.jar tests/*.java
However I can't seem to run tests:
java -cp build/jar/jar_file.jar:lib/junit-4.10.jar org.junit.runner.JUnitCore tests.Tester
or
java -cp build/jar/jar_file.jar:lib/junit-4.10.jar org.junit.runner.JUnitCore Tester
And I get the following output:
JUnit version 4.10
Could not find class: tests.Tester
Time: 0.001
OK (0 tests)
How do I resolve this Could not find class problem? I think it may be classpath related.
Assuming this is Linux/Mac (not Windows) and your path separator is correct (:), since your test class files exist in package subdirectories under the current working directory (.)
You need to add "." to your class path, for example:
java -cp .:build/jar/jar_file.jar:lib/junit-4.10.jar org.junit.runner.JUnitCore tests.Tester
The classpath should be semi colon separated (on Windows - not sure what you are using.)
java -cp build/jar/jar_file.jar;lib/junit-4.10.jar org.junit.runner.JUnitCore tests.Tester
Also with this command line you will need to run it in your project root
I am on Arch Linux, I just installed JRE and JDK and all the proper bin files (javac and java) are in /opt/java/bin/
I simply compiled a standard hello world, and compiled it with javac running javac ./hello.java and that made a class.
Now my problem is running it. I run java ./helloworld.class and it gives me an error, even if the file I point java to is non-existant:
Exception in thread "main" java.lang.NoClassDefFoundError: //helloworld/class
Caused by: java.lang.ClassNotFoundException: ..helloworld.class
(..omitted for clarity..)
Could not find the main class: ./helloworld.class. Program will exit.
You will notice the first line of the error, it munges the path //helloworld/class
When I feed java an absolute path, i.e java /home/foo/helloworld.class it gives the same error, but replaces the path's / with . in the first line, again munged.
What do you think is wrong? I really don't know why it is doing this..
When you run java, you just pass it the fully qualified class name (including package), not the file name.
java helloworld will look for helloworld.class.
java helloworld.class will look for helloworld/class.class
You do not run a file as
# java file.class
you run it as
# javac PATH/file.java
# java PATH/file
Do not add .class while using JAVA command.
Actually you should compile it like this
javac helloword.java
run the program
java helloword
And yet another thing: add command line option "-classpath ." or it short version "-cp .", i.e. your command line should look like:
java -cp . helloworld
this is if your class is in your current directory. Otherwise "." should be replaced by path where the class(es) may be found.