I know about Groovysh, but I need to know if it is possible to run groovy CLI directly from groovy.jar placed in deployed war servlet (using one command). I can simplify question. Is there possibility to run Groovy CLI like it works, for example, in Clojure?
java -cp clojure-1.4.0.jar clojure.main
And CLI appears in terminal. This is how things look in clojure. I am looking for one line command which will run groovy CLI in terminal (using only groovy library to run it). I was looking for help in javadoc, found classes which should help, but don't know how to run it. :f
http://groovy.codehaus.org/api/groovy/lang/GroovyShell.html
(If someone knows solution which doesn't meet all criterias, also please answer.)
//EDIT
It seems that it needs groovy.jar, commons-cli.jar, antlr.jar, asm-util.jar and jline.jar. So I've added those files in my war file in WEB-INF/lib directory. Maybe it's good solution to make my own jar which role will be to call Groovy CLI from other jars, but now the question is, how ro run jar placed in WEB-INF/lib directory inside deployed war application via command line?
Greets
Seems like groovysh depends on more libs than the embeddable version contains. I managed to do what you want by using the following command:
$ java -classpath '*' org.codehaus.groovy.tools.shell.Main
But the current directory had to be the dir where all the groovy libs are; i.e. $GROOVY_HOME/lib:
$ cd $GROOVY_HOME/lib
$ java -classpath '*' org.codehaus.groovy.tools.shell.Main
Groovy Shell (2.0.6, JVM: 1.6.0_24)
Type 'help' or '\h' for help.
------------------------------------------------------------------------------
groovy:000>
Related
I wrote a small Scala application. I have 2 classes in one source file including the App trait runner to start the program. It works just fine when I run it in the terminal:
scalac update.scala // compiling
scala update // run it
Now I want to run it with a cron job. For this I edited sudo crontab -e and added this:
*/2 * * * * scala /usr/bin/local/update
and made the script executable but nothing happend so far. I'm not sure how to do it:
Do I have to make a jar file for this?
Do I have to add this before my classes or not?
#!/bin/sh
exec scala -savecompiled $0 $#
!#
Does anyone have some experience with this?
Thanks in advance.
I suspect scala isn't in $PATH where cron can see it.
Try the following in a shell session:
$ which scala
Which should output something like "/opt/scala/2.9.1/bin/scala" or something. Could be in /usr/local, any number of places - java and the unix filesystem don't really play together nicely.
So now you have two options:
Put the folder where scala lives in the system path (This will usually involve editing /etc/profile, but you don't specify the OS so I can't say for sure)
(Easier) Just change the the cron entry to call /full/path/to/scala rather than just "scala"
The scala command expects the name of a compiled runnable object or a file containing a scala script source (or a runnable jar file) as the thing to run.
If you have in update.scala object update extends App (and no package declaration) then after scalac update.scala (which should have produced a bunch of *.class files) scala update is the right thing to run.
If the produced class files are not in the current directory then the -classpath option should be used to tell scala where to find them, as in eg. scala -classpath /usr/bin/local update, if the class files are indeed in /usr/bin/local.
Saying scala /usr/bin/local/update would make sense if the file /usr/bin/local/update (this exact name) contained scala script source (that is more or less a sequence of scala expressions not wrapped in a class or object).
Invoking a groovy script using CLASSPATH prefix as follows works fine:
CLASSPATH=/path/to/classes groovy -e "(new stuff.XMLUtils()).printIt('test string')"
but changing it to use the classpath arg doesn't:
groovy -classpath /path/to/classes -e "(new stuff.XMLUtils()).printIt('test string')"
and gives the error:
script_from_command_line: 1: unable to resolve class stuff.XMLUtils
Can anyone explain why this is? (The stuff.XMLUtils is just some groovy script i've compiled into /path/to/classes
)
I've done some investigation, and using the following groovy script to dump the classloader
def printClassPath(classLoader) {
println "$classLoader"
classLoader.getURLs().each {url->
println "- ${url.toString()}"
}
if (classLoader.parent) {
printClassPath(classLoader.parent)
}
}
printClassPath this.class.classLoader
With the -classpath arg, i see no entry in the classloader for the passed in classpath arg, (in fact, the only directory is the current working dir), e.g.:
groovy.lang.GroovyClassLoader$InnerLoader#4911b910
groovy.lang.GroovyClassLoader#18203c31
sun.misc.Launcher$AppClassLoader#35a16869
- file:/usr/share/java/ant.jar
- ... (removed for brevity)
- file:/home/admin/groovy/
sun.misc.Launcher$ExtClassLoader#77cde100
- file:/usr/java/jdk1.6.0_23/jre/lib/ext/sunjce_provider.jar
- ...
Using the CLASSPATH=... version shows that the PWD entry above is replaced by the value i've set in the variable.
And if I add debug to the groovy shell executable, the difference in the java call is that the -classpath arg version adds no entry to java's classpath entry (which is ultimately why it's giving a class not found error), but the CLASSPATH=... version does add the path.
Is this a bug in groovy?
EDIT: simple failing example
- - - - xu.groovy
package stuff
def printIt(string) { println string }
- - - -
groovyc -d classes xu.groovy
groovy -cp classes -e "(new stuff.xu()).printIt('test')" # fails
CLASSPATH=classes groovy -e "(new stuff.xu()).printIt('test')" # works
If I remove the package and references to stuff the failing example will work fine.
Answering this myself because I found a solution to the problem.
I was using the default groovy packages from yum in fedora, however found many issues (errors starting groovysh etc, unable to find jline package etc), and have wholly moved over to using downloaded versions from codehaus.org, and manually specifying GROOVY_HOME and editing path to invoke the downloaded one instead.
Now all my examples work as expected.
I'm on MSYS/Win32 + groovy 2.2 RC1 and have another twist:
groovy -cp "./*" script.groovy // Works!
but
groovy -cp some.jar script.groovy // ... not
For some reason, none of the above would work out in my case.
That's strange. I just tried to repeat your explained issue but everything seems to work fine (I made tests with Groovy-Version 1.8.6, 1.7.7 and 1.7.0 on my Ubuntu Computer).
So which version do you use and what is your operating system?
In the Groovy Bug Tracker I have found the following bug: Command line option for classpath (--cp/--classpath) is broken on Windows. But this bug just affects old versions of Groovy (1.5.2, 1.5.3 and 1.5.4). So maybe an upgrade of Groovy will help to fix your problem...
PS: Normally I just would comment this, but unfortunately I haven't enough points for doing this :).
I'm trying to run an ant build in linux and I'm getting odd results. I can't seem to get anything to run without specifying each and every library on the classpath, so I systematically added every jar in the "lib" and "ant/dep/lib" locations to the classpath explicitly.
It runs a bit without any issues (it's a setup batch, so in the first phase of the script, it uses a lot of "input" tasks), then I receive the message that org.apache.tools.ant.taskdefs.optional.PropertyFile was not found. Looking around, I discovered it was located in ant-nodeps.jar and I had added it to the class path, but no change.
Complete failure due to missing libraries I can understand, but why would the build partially function if I was sure I was including every library (including ant-nodeps.jar)?
The script file to launch it is as follows:
#!/QOpenSys/usr/bin/bsh
ANT_HOME=dep/ant
PARAMS="-Dant.home=$ANT_HOME -Dsystem.type=as400 -Dis-as400=y"
LIBS="lib/ant-contrib-1.0b3.jar:lib/ant-launcher.jar:lib/ant.jar:lib/catalina-ant.jar:lib/catalina-deployer.jar:lib/commons-net-2.2.jar:lib/el-api.jar:lib/ganymed.jar:lib/jakarta-oro-2.0.8.jar:lib/jasper-el.jar:lib/jasper.jar:lib/jsp-api.jar:lib/log4j-1.2.16.jar:lib/mail.jar"
ANTLIBS="$ANT_HOME/lib/ant-antlr.jar:$ANT_HOME/lib/ant-jai.jar:$ANT_HOME/lib/ant-starteam.jar:$ANT_HOME/lib/ant-apache-bcel.jar:$ANT_HOME/lib/ant-javamail.jar:$ANT_HOME/lib/ant-stylebook.jar:$ANT_HOME/lib/ant-apache-bsf.jar:$ANT_HOME/lib/ant-jdepend.jar:$ANT_HOME/lib/ant-swing.jar:$ANT_HOME/lib/ant-apache-log4j.jar:$ANT_HOME/lib/ant-jmf.jar:$ANT_HOME/lib/ant-testutil.jar:$ANT_HOME/lib/ant-apache-oro.jar:$ANT_HOME/lib/ant-jsch.jar:$ANT_HOME/lib/ant-trax.jar:$ANT_HOME/lib/ant-apache-regexp.jar:$ANT_HOME/lib/ant-junit.jar:$ANT_HOME/lib/ant-weblogic.jar:$ANT_HOME/lib/ant-apache-resolver.jar:$ANT_HOME/lib/ant-launcher.jar:$ANT_HOME/lib/ant.jar:$ANT_HOME/lib/ant-commons-logging.jar:$ANT_HOME/lib/ant-netrexx.jar:$ANT_HOME/lib/xercesImpl.jar:$ANT_HOME/lib/ant-commons-net.jar:$ANT_HOME/lib/ant-nodeps.jar:$ANT_HOME/lib/xml-apis.jar"
java -classpath $LIBS:$ANTLIBS $PARAMS org.apache.tools.ant.launch.Launcher -buildfile install/install.xml
Any help would be appreciated, even if only suggestions for what to try.
EDIT: In light of oers observation, I realized that there are shell scripts for ant afterall (have pity, I'm no expert in linux), so I'm going to attempt to use it to launch ant rather than try to do the same thing myself. I'll let everyone know of my progress and/or if I was able to fix it doing it that way.
I looked into the shellscript on my machine.
There is an additional parameter set:
-Dant.library.dir=\"$ANT_LIB\"
Perhaps this is needed?
ant_exec_command="exec \"$JAVACMD\" $ANT_OPTS
-classpath \"$LOCALCLASSPATH\"
-Dant.home=\"$ANT_HOME\"
-Dant.library.dir=\"$ANT_LIB\" $ant_sys_opts
org.apache.tools.ant.launch.Launcher
$ANT_ARGS
-cp \"$CLASSPATH\"
$ant_exec_args"
Using the ant scripts to launch the setup seemed to do the trick, specifying parameter -lib as lib and -buildfile as install/install.xml.
The following that I have to use to run my groovy class file
..\bin\groovy.bat -cp ....;%classpath%;..\lib; GetTemplateNode.groovy
I am confused with .... part in the command before the groovy.bat and after the -cp.
Can you please explain me what each part of this command actually stand for?
Thank you
The .... is probably a mistake. Other than that, the command is starting the GetTemplateNode.groovy script, and adds the lib folder, that is located one folder above, to the classpath.
I'm trying to setup a kiosk type system in linux where a java application launches when X is initialized. I've got a script which does the following:
java -cp {correct path to JAR with main method} -jar {name of JAR}
When I've cd'd into the directory where the JAR sits, everything is peaches and the system works like I want, however, if I navigate to any other directory, X terminates and reports that it could not access the JAR.
What am I missing here? Any help is greatly appreciated.
You don't need the -cp and the -jar
java -jar {full path}
should work. Your mainifest file in the .jar should then specify the appropriate main() method. See here for more details on that.