pass "git describe" output to Java code - java

Is there a way to pass the output of git describe --tag to my Java application at compile time? I'd like to use that in my about box to display version info. I'm using Eclipse to build, but I can use ant if necessary.
With C code, I can pass -D'REV="$(shell git describe --tag)"' to my gcc compiler, and my code can pick that up. I'm hoping there's something similar in Java.

If ant is acceptable, it supports a similar command line option.
As a concrete example, this line in an ant script
<echo>property: ${property}</echo>
when invoked with the following command,
$ ant -Dproperty="$(git --version)"
produces the following output on the console,
[echo] property: git version 1.7.5.4

Related

Why exec-maven-plugin fails while java console command works?

This problem has arisen while working with the Java Chromium Embedded Framework (JCEF).
The JCEF uses a native SO library (libcef.so) and a SO library for the JNI bindings (libjcef.so). I believe the embedded Chromium looks for a file named icudtl.dat next to it, i.e. in the same directory that the libraries reside.
For the following explanation, asume that all those required files are in the same directory, named jcef_libs.
I wrote a test application using JCEF. After compiling, if I execute it with the java command, i.e.
$ export LD_LIBRARY_PATH=/path/to/jcef_libs
$ java -cp src/main/java:/path/to/maven/repo/jcef.jar:/path/to/maven/repo/jogl-all/2.2.4/*:/path/to/maven/repo/gluegen-rt/2.2.4/* tests.simple.MainFrame
it works flawlessly.
On the other hand, using the exec-maven-plugin raises a fatal error, due to not finding the named icudtl.dat file. The POM file is configured to use exactly the same artifacts as the java command does.
$ mvn exec:java -Dexec.mainClass="tests.simple.MainFrame"
[1201/123038:FATAL:content_main_runner.cc(721)] Check failed: base::i18n::InitializeICU().
I have tested the java command with the icudtl.dat file removed from the jcef_libs directory, experiencing the same error, i.e. the error raises only when the icudtl.dat is not found.
Any help will be appreciated.
UPDATE
I have been tracing what maven does under the hood. The failing mvn command above executes this internally:
java -classpath /usr/share/maven2/boot/classworlds.jar -Dclassworlds.conf=/usr/share/maven2/bin/m2.conf -Dmaven.home=/usr/share/maven2 org.codehaus.classworlds.Launcher "exec:java" "-Dexec.mainClass=tests.simple.MainFrame"
I.e. it executes my test application through a launcher. I don't know where this launcher is going to execute my application but, again, I need the icudtl.dat in that directory.
Any idea how could I workaround this?

running nashorn using Shebang does not accept -cp option

I am trying to use a class into a jar using a Nashorn Shebang script with the -cp option (java version "1.8.0_31"). However it does not works. I have perform some test. The following shebang line works:
#!/usr/bin/jjs -scripting
#!/usr/bin/jjs -fv (returns nashorn full version 1.8.0_31-b13)
while the following not:
#!/usr/bin/jjs -cp ./some/lib/lib.jar will return the following error message: "-cp ./some/lib/lib.jar" is not a recognized option.
#!/usr/bin/jjs -scripting -fv will return the error message: "-scripting -fv" is not a recognized option. Use "-h" or "-help" to see a list of all supported options"
All options are theoretically valid. The classpath option should also works as seen on http://www.adam-bien.com/roller/abien/entry/setting_the_classpath_for_nashorn .
More info about nashorn and Shebang: http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/shell.html#CHDEGHJJ
You ran into an issue which doesn’t have anything to do with Nashorn nor Java. According to this answer the command line argument handling with shebang never was clearly specified and it seems to be a common behavior to treat everything encountered after the first white-space as one single argument.
So one solution would be to write a shell script containing an invocation of jjs with the actual arguments and use that shell script as the interpreter in the shebang line of your Nashorn script.
You can use -Dnashorn.args=-cp in a shebang script. See also https://bugs.openjdk.java.net/browse/JDK-8072138
As it appears you want a way to automatically add JARs to your classpath, I'll highlight a little wrapper I've written which allows you to define Maven co-ordinate dependencies (including transitives) to be added to the classpath of your script, so you can write a script using the "# dep" lines:
#!/usr/bin/env jjs-with-deps
#
# The line below is parsed by the jjs-with-deps script to build a new
# classloader in which the script is really executed, including logback
# and its transitive dependencies.
#
# dep:ch.qos.logback:logback-classic:1.1.2
var log = org.slf4j.LoggerFactory.getLogger("com.example.app.Logger");
log.info("Hello World!");
It does require Maven to be installed somewhere on your PATH, and it does slightly increase startup time (but then again, you're already starting up a JVM ;). First invocation of a given script will be much slower while any dependencies are downloaded to the local M2 repository.
Link is https://github.com/stevestorey/jjs-with-deps

Install protoc without maven (win7, java)

I tried installing protoc on windows without maven (I can't download maven due to org firewall issues).
I have my protoc.exe in the ..\src (not in java\src as many do) folder as mentioned in the readme.
When I give protoc --version in the command prompt, I get libprotoc 2.6.0. That's fine.
But when I give protoc --java_out=src/main/java -I../src/google/protobuf/descriptor.proto, I get missing input file all the time & I can't proceed further.
I have tried giving the entire path where my descriptor.proto lies, tried changing -I to -IPATH, tried using --proto_path but I still get the same Missing input file error.
Can someone help me out on this?
The command you want (from the Protobuf-Java readme) is:
protoc --java_out=src/main/java -I../src ../src/google/protobuf/descriptor.proto
Notice that the -I flag and the descriptor.proto argument are two different arguments. It looks like you accidentally merged them into one.

How to use jruby-complete programatically from Java?

I've followed the following article http://seanchenxi.com/java/sass-compass-jruby-single-jar/ on how to get a jar with ruby, sass and compass.
I'd like to use this as part of a Jenkins plugin. Is there a way to call this programatically from Java the same way that you would run from the command line java -jar jcompass.jar -S compass compile --help?
Warbler can do that ... try packaging up with the runnable feature and see how it goes :
java -jar jcompass.jar -S compass compile
... even if it does not work please report with the project - should be "easily" fixable

changes to java program not being reflected

#!/bin/ksh
echo "Some Programme v1.0.0"
JAVA_HOME=/apps/clear/jdk1.7.0_45
PATH=${JAVA_HOME}/bin:${JAVA_HOME}/lib:/usr/local/bin:/bin:/usr/bin:.:
export NEWAPI_DIR=/local/newapi/1.1.1.1.2
LIBRARY_PATH=$NEWAPI_DIR/Linux-2.6/lib
MY_HOME=/home/clear/dev/app/lse
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$LIBRARY_PATH
MY_CLASSPATH=${MY_HOME}/lib/yt500.jar:${JAVA_HOME}/jre/lib/rt.jar:${MY_HOME}/lib/ftlse.jar:$LIBRARY_PATH/JNewApi.jar:$LIBRARY_PATH/Jfib.jar
date
$JAVA_HOME/bin/java -version
$JAVA_HOME/bin/java -classpath $MY_CLASSPATH com.company.ft.lse.LseParser /home/clear/dev/app/lse/config/config.xml
date
The above is my run script. I have an application LseParser.java to which I have changed the code. But this code is not reflected when I have run the code (. ./run) in unix box. When I locate the ftlse.jar within where the LseParser.class exists, it is last date modified some long time ago. Can someone please point me what i'm doing wrong or suggest any checks or alterations I should make ? am i missing any lines in my script i should have ?
Java is a compiled language. You need to run the javac compiler first and then the jar command to create the jar file. You can use the tutorials I linked to to figure out how to run them correctly for your project. Once you have your compile and package commands you can add them to your run script. However, I strongly recommend you look into a build automation tool such as Maven so that you don't have to work with custom built run scripts.
I think you missed export before JAVA_HOME in 3rd line... so the shell, instead of taking $JAVA_HOME it as a variable, interpret it as a command.

Categories