How to invoke groovy with 'java' from command line - java

I have to ship some groovy code to some users that have only java installed (no grooy, no $groovy_home, etc). I'm trying to invoke groovy from the commandline but I'm having no luck. Here's my bat file:
java -classpath .;lib;bin;bin-groovy introspector.AclCollector
And here's my exception:
Exception in thread "main" java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Caused by: java.lang.ClassNotFoundException: groovy.lang.GroovyObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 12 more
Could not find the main class: introspector.AclCollector. Program will exit.
Does anyone have a clue? I have 'groovy-all-1.6-beta-1.jar' in \lib dir.

You have here another example of Groovy app called from Java (in this case, from ant, but the general idea is the same).
java -cp [...];%GROOVY_HOME%/embeddable/groovy-all-1.5.4.jar;[..]
As mentioned by frankowyer, you have the exact groovy jar explicitly listed on the classpath arguments of the java.
Since your clients do not have special environment variable, just replace the %GROOVY_HOME%/... with the complete path to:
groovy.jar or
goovy-all-VERSION.jar (to minimize any conflict with other libraries)

I think you need to explicitly list the groovy jar in the classpath

One way to avoid problems with different class paths on different machines would be to bundle all the necessary dependencies into one single jar, this would also make distribution to users easier. This can be done with this 'GroovyWrapper' script. The default jars (embeddable/groovy-all-.jar and lib/commons.jar) are included by default in the script and if you require other JARS they can easily be added.
See http://docs.codehaus.org/display/GROOVY/WrappingGroovyScript for the full script and instructions.
Here's an example of how to use GroovyWrapper:
Say you have groovy script HelloWorld.groovy, use GroovyWrapper for building HelloWorld.jar, as follows:
$ groovy GroovyWrapper -c -m HelloWorld
GroovyWrapper will compile the script HelloWorld.groovy to HelloWorld.class, and creates a self-executable jar HelloWorld.jar.
Now you can use the HelloWorld.jar for launching the HelloWorld script, simply by running:
$ java -jar HelloWorld.jar

GREAT ANSWER by VonC:
....... Since your clients do not have special environment variable,
just replace the %GROOVY_HOME%/... with the complete path to:
groovy.jar or
goovy-all-VERSION.jar (to minimize any conflict with other libraries)........
this is for windows Vista:
In System Properties | Environmental Variables | User variables:
Variable Name GROOVY_HOME set to C:\groovy\Groovy-1.8.5
and the Variable Name CLASSPATH value's set to include .;%GROOVY_HOME%\embeddable\groovy-all-1.8.5.jar
Don't make the mistake I did(spinning my wheels a bit), of not prep-ending the path of the 'groovy-all' with ".;" (dot and semi-colon): as the path needs the base folder you're in for finding the main class(strange as it seems)

Watch out of [~]!
java -cp .:~/path-to-groovy-all.jar YourClassName # does not work
java -cp ~/path-to-groovy-all.jar:. YourClassName # works
java -cp .:/full/path/to/goovy-all.jar YourClassName # works
In first line tilde is not processed by bash, and java can not understand it.
In second line tilde is replaced by bash and everything works fine.

Related

running a java project from command line using classpath issue?

I am trying to run a java project from the command line in linux, my project uses two external jar files. The command that i am givin is
java -classpath -jar bin:common-cli-1.2.jar:BuildFrameworkLibrary.jar com.kpit.goa.common.tools.kivibuild.KIVIBuild
where KIVIBuild is the class that contains the main function. But the error that am getting is:
baibhav#baibhav:~/git/KiviBuild/Infra/RepoManagement/BuildManagement/KIVIBuild$ java -classpath bin:common-cli-1.2.jar:BuildFrameworkLibrary.jar com.kpit.goa.common.tools.kivibuild.KIVIBuild
Gives
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: com.kpit.goa.common.tools.kivibuild.KIVIBuild. Program will exit.
You need a path separator e.g.
bin/common-cli-1.2.jar:BuildFrameworkLibrary.jar
The colon separates paths to individual jar files. e.g. in the above you're referencing the two files
bin/common-cli-1.2.jar
BuildFrameworkLibrary.jar
and also reference the directory containing your compiled classes e.g. if they're in (say) target/classes then use:
target/classes:bin/common-cli-1.2.jar:BuildFrameworkLibrary.jar
(relative to your current working directory)
Use following command
javac -classpath bin/common-cli-1.2.jar:bin/BuildFrameworkLibrary.jar KIVIBuild.java -d .
Do not forget to add dot at the end of command
check is /com/kpit/goa/common/tools/kivibuild/KIVIBuild.class exist or not
if yes run the following command
java -classpath bin/common-cli-1.2.jar:bin/BuildFrameworkLibrary.jar com/kpit/goa/common/tools/kivibuild/KIVIBuild
http://www.linuxheadquarters.com/howto/classpath.shtml
Rather than setting class path every time set it onces in existing classpath variable and just run the java command

Works in sbt but not raw Java: after using `javac` to compile a .class file, why can't `java` find it?

I used ObjectWeb's ASMifier to get a 'HelloDump.java', and added classloader and a main method to load and run a spoofed "HelloWorld".
If I run 'HelloDump.java' in the build tool sbt, everything works fine and outputs "HelloWorld!".
But if I use raw Java, it breaks. 'HelloDump.java' seems to compile OK, but the resulting 'HelloDump.class' is clearly present, yet doesn't seem to be recognized (check the sequence below):
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ javac -cp lib/asm-all-4.1.jar HelloDump.java
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ ls
DumpLoader.java.bak HelloDump.class.bak Hello.java.bak
Hello.class.bak HelloDump$DynamicClassLoader.class lib
HelloDump.class HelloDump.java README.md
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ java -cp lib/asm/all/4.1.jar HelloDump
Exception in thread "main" java.lang.NoClassDefFoundError: HelloDump
Caused by: java.lang.ClassNotFoundException: HelloDump
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: HelloDump. Program will exit.
Explicitly adding . to the classpath doesn't help either.
So why does this work in sbt, but not in raw Java, and how can I fix it?
Thanks, any advice is appreciated,
-Julian
Add . to your CLASSPATH, so your command becomes:
java -cp lib/asm-all-4.1.jar:. HelloDump
The reason the JVM cannot find the class is that it looks only on the classpath and nowhere else.

Javac compilation and execution of 2 java files

The program is written with 2 files filter.java - in which is the main function and ClasifiedWord.java which is only container class. I wrote that on windows on eclipse but want to compile it on Linux javac compiler.
javac filter.java ClasifiedWord.java
runs without mistakes, but when I try to run the program:
snowy:Filter$ java filter
Exception in thread "main" java.lang.NoClassDefFoundError: filter
Caused by: java.lang.ClassNotFoundException: filter
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: filter. Program will exit.
snowy:Filter$ javac filter.java ClasifiedWord.java
snowy:Filter$ java filter ClasifiedWord
Exception in thread "main" java.lang.NoClassDefFoundError: filter
Caused by: java.lang.ClassNotFoundException: filter
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: filter. Program will exit.
How can I run the program ? It seems that the compilation is ok. I have doubts that maybe I have made a mistake about the file paths in the program.... but I think that this is not the case?
Thanks for your answers!
Type the following command.
java -classpath . filter
If you want to type just java filter, please following these guides.
Linux http://www.cyberciti.biz/faq/linux-unix-set-java_home-path-variable/
Windows http://ac-support.europe.umuc.edu/~arnoldyl/NetBeansTutorials/Setting-Classpath.html
P.S. The last command java filter ClasifiedWord means to run a program filter and use a string ClasifiedWord as an argument.
Roland is right in the comment, you should be able to run it with java -classpath <folderContainingDotClassFiles> filter. Also, you should change filter.java to Filter.java as the standard Java convention for class names (including the files containing them) is to capitalize the first letter.
This looks like you either have a wrong CLASSPATH setting (usually you don't need this at all, but if you have it, it should include . in the list, for the current directory), or you are using packages (wrongly), or both. For the classpath issue, see the other answers.
If you are using packages, i.e. your files contain a package ...; line at the beginning, you should put your source files in a directory structure according to the package structure. For example, if you have package example;, you should have an example directory in your current directory, and in this the two source files.
Then call compiler and interpreter like this:
javac example/filter.java example/ClasifiedWord.java
java example.filter
(This assumes that filter is the main class, otherwise replace its name.)

running scala apps with java -jar

I got some problems with the java. Check it out.
sebastian#sebastian-desktop:~/scaaaaaaaaala$ java -cp /home/sebastian/.m2/repository/org/scala-lang/scala-library/2.8.0.RC3/scala-library-2.8.0.RC3.jar:target/scaaaaaaaaala-1.0.jar scaaalaaa.App
Hello World!
That's cool, right, but how bout this:
sebastian#sebastian-desktop:~/scaaaaaaaaala$ java -cp /home/sebastian/.m2/repository/org/scala-lang/scala-library/2.8.0.RC3/scala-library-2.8.0.RC3.jar -jar target/scaaaaaaaaala-1.0.jar
Exception in thread "main" java.lang.NoClassDefFoundError: scala/Application
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at scaaalaaa.App.main(App.scala)
Caused by: java.lang.ClassNotFoundException: scala.Application
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 13 more
Wat the heck? Any idea why the first works and not the 2nd? How do I -jar my scala??
My thanks in advance, brother.
If you define -jar the -classpath is ignored:
Java manual:
-jar When you use this option, the JAR file is the source of all user
classes, and other user class path
settings are ignored.
You can define the classpath dependencies in the Manifest metadata.
The easiest way to start your app is using the scala scripts:
scala -classpath target/scaaaaaaaaala-1.0.jar scaaalaaa.App Hello World!
Simply running
scala scaaaaaaaaala-1.0.jar
works for me with 2.11.6
use sbt-assembly sbt plugin to produce a jar containing all dependencies
sbt-assembly github
e.g. add a line in project/plugins.sbt:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10")
adjust your build.sbt e.g. with
mainClass in (assembly) := Some("mypackage.mysubpackage.MyMainClass")
assemblyJarName in assembly := "my_assembly.jar"
then build your assembled jar with
sbt assembly
and launch it with
java -jar ./target/scala-2.12/my_assembly.jar
Et voilĂ , no class-path this & thats needed any more. only 1 jar.
For an executable jar, the classpath should be in the jar's manifest. For help on doing that, look through the answers to Stackoverflow: How to create an executable jar with dependancy jars.
Below is the way to execute the Scala Executable Jar
We need scala installed in your system.
Here first will give the jar path and jar name
If your code needs any dependency jar then keep all jar in one directory and give a path of this in command like below after the ":".
At last give your class/object name.
scala -cp "/your/jar/path/jar_name.jar:/your/dependency/jar/path/*"
SampleCode

NoClassDefFoundError without any class name

I am trying to run a java task from ant. I am trying to run the "org.apache.tools.ant.launch.Launcher" class. I keep on getting the "NoClassDefFoundError" without any class name being specified. I am also getting a "ClassNotFoundException" along with that displaying a message "Could not find the main class: . Program will exit". Here's a snippet of the error
[java] Exception in thread "main" java.lang.NoClassDefFoundError:
[java] Caused by: java.lang.ClassNotFoundException:
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
[java] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
[java] Could not find the main class: . Program will exit.
[java] Java Result: 1
Now I am trying to run an ant class from an ant jar and i specifiy the classpath where this class file resides using the "classpathref" attribute, however I still get this message. I checked the ant jar to check the Manifest and the "main" class is specified properly (it's "org.apache.tools.ant.launch.Launcher") . I have exhausted all my resources. Please help ! ! !
ps: My environment is Eclipse on Ubuntu 9.04
Most likely your classpath is misconfigured.
At a minimum the CLASSPATH should include:
ant.jar and ant-launcher.jar
jars/classes for your XML parser
the JDK's required jar/zip files
(from the ant manual)
Also you seem to be relaunching ant in the current directory (executing the same build.xml). Maybe you'll want to set the "dir" property.
It looks like the Ant task is trying to run Java, but is somehow passing an empty string to the JVM as the name of the class to run. I can get the same stacktrace if I run the JVM directly with a quoted empty string:
C:\>java ""
Exception in thread "main" java.lang.NoClassDefFoundError:
Caused by: java.lang.ClassNotFoundException:
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: . Program will exit.
(This is on Windows, but I don't think that makes much of a difference.)
I can only suggest following up on Alexander Pogrebnyak's comment to akf's answer. Perhaps the webtest.lib property has spaces in it?
Also, is there a good reason for calling ant directly via java, rather than using the ant task?
https://blogs.oracle.com/sreekanth/entry/java_lang_noclassdeffounderror_org_codehaus
java.lang.NoClassDefFoundError: org/codehaus/plexus/classworlds/launcher/Launcher
By sreekanth on Nov 23, 2010
Recently when I am trying to run some Maven scripts, I am getting this exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/plexus/classworlds/launcher/Launcher
Caused by: java.lang.ClassNotFoundException: org.codehaus.plexus.classworlds.launcher.Launcher
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: org.codehaus.plexus.classworlds.launcher.Launcher. Program will exit.
After spending some time trying various combinations , I found that this is because I have both M2_HOME and M3_HOME set in my environment variables.Once I removed M2_HOME from my environment variables, I could get this working back again.May be this could save some serious time for some one.
This can be a misleading error that is not actually about a class missing from the classpath. If you are using Tomcat it can be due to missing conf files in $CATALINA_BASE/conf
It could also be a misconfigured ant installation, please check your JAVA_HOME and ANT_HOME env variables or try another ant installation.
Ant launcher expects the following params
java -Dant.home=c:\ant org.apache.tools.ant.launch.Launcher [options] [target]
I am affraid we can't proceed answering you if you don't paste your whole build.xml file.
Just try to give your full sample as below:
<java
classname="org.apache.tools.ant.launch.Launcher"
fork="true"
failonerror="true"
dir="${sub.builddir}"
timeout="4000000"
taskname="startAnt"
>
<classpath>
<pathelement location="${ant.home}/lib/ant-launcher.jar"/>
</classpath>
<arg value="-buildfile"/>
<arg file="${sub.buildfile}"/>
<arg value="-Dthis=this"/>
<arg value="-Dthat=that"/>
<arg value="-Dbasedir=${sub.builddir}"/>
<arg value="-Dthe.other=the.other"/>
<arg value="${sub.target}"/>
</java>
This would be extremely helpful to provide you with a possible misunderstanding.
Hope this helps,
Ernani
From this line:
[java] Could not find the main class: . Program will exit.
it looks as though your call to java.exe is finding a . where it expects a class name. Perhaps you are trying to indicate the classpath on the commandline but are neglecting to preface that with the -cp or -classpath flag.
When in doubt, invoke ant -v and watch all your variable declarations, and the whole commandline sent to Java.
Certain path-like quantities are eagerly evaluated, while others are lazily evaluated. I've had plenty of problems where I used one of the former, when my Ant script intended to create a jar that would be used by a later task. Then by the time I invoked the call, it had already pruned my jar from the classpath.
If I had to make a wild guess, I'd bet your commandline looked something like:
java ... -classpath org.apache.tools.ant.launch.Launcher
instead of
java ... -classpath foo.jar;bar.jar org.apache.tools.ant.launch.Launcher
like you expected
I had a similar problem recently. The culprits were 2 tags under the java task, that didn't have their values set, so they resulted in 2 empty command arguments and in the end in 2 spaces in the command line.
For some reason Unix doesn't handle it right. Both Red Hat 5 and Ubuntu displayed the same error. It was OK on Windows 7.
Setting those arguments to have default dummy values solved the issue.

Categories