jar-with-dependencies + Multiple mainClass classes? - pom.xml

I know how to create a myapp-with-dependencies.jar with a single mainClass.
But I want to create myapp-with-dependencies.jar with support for different main classes at runtime. Running the different classes shoudl be as simple as this:
java -jar myapp-with-dependencies.jar com.myapp.MyApp1
java -jar myapp-with-dependencies.jar com.myapp.MyApp2
I believe with a thin jar you can do this, but then you have to specify --classpath which I don't want to do (it complicates my deployment script).
Is this possible? If not, what's the next best thing (besides running it like a thin jar, or creating separate jars). And maybe an explanation of why this is impossible would be beneficial for me and others reading this.

Actually, it's perfectly fine for me to use a classpath where the fat jar contains everything:
java -cp myapp-with-dependencies.jar com.myapp.MyApp1
java -cp myapp-with-dependencies.jar com.myapp.MyApp2

Related

Dropwizard - running a standalone command without adding bundles

I have a Dropwizard app with swagger-jaxrs2 generating a openapi spec. I would like to have a command run this simply as such java -jar app.jar generate build/spec.json for example, but because I have Hibernate configured, it requires me to specify the configuration file even though it is irrelevant to the command completely, and NPEs.
Trying to specify the configuration leads to this error:
unrecognized arguments: 'src/test/resources/configs/configuration.test.yml'
Because I haven't explicitly added the argument for the configuration.
Is there no way around this? I have to specify the config every time? Just really annoying :D
Edit: ended up just hijacking main and outputting it that way since I only need access to the classpath in this instance. Idea courtesy of Dropwizard command line input
You could define a special command extending io.dropwizard.cli.Command, which doesn't require passing configuration as paramater, since it is not a io.dropwizard.cli.ConfiguredCommand.
The just register it in your override of io.dropwizard.Application#initialize and call bootstrap.addCommand(new YourSpecialCommand());
It would allow you to call:
java -jar app.jar key-of-your-special-command <any args you define in your command>

java -jar saxon9he.jar persons.xml persons_users.xslt -o:persons_transformed.txt

I am having an XSLT to convert my xml into html format. I didn't achive any Experience about Saxon before but I'll try again and again.
This is the problem I had :
C:>java -cp saxon9he.jar net.sf.saxon.Transform -t -s:samples\date\books,xml-csl:samples\styles\books,xsl -o:c:\temp.html
Error: Main class net.sf.saxon.Transform could not be found or loaded
I did everything step by step from the Saxon Website :
https://www.saxonica.com/html/documentation/about/gettingstarted/gettingstartedjava.html
and I saw MR.Michael Kay Videos a lot before but it isn't work any way.
Can perhaps any one help me please ?
The problems are with Java, not with Saxon, in case that helps you look in the right place for documentation.
The message "Main class net.sf.saxon.Transform could not be found or loaded" is Java telling you that it can't find Saxon.
The bit of the command that tells it where to look is this:
java -cp saxon9he.jar net.sf.saxon.Transform
Here "java" is telling the operating system to load the Java virtual machine (which has succeeded). The "-cp" option is telling Java what Jar files to search for the relevant classes, and the "net.sf.saxon.Transform" part is saying what the relevant class is.
The problem is probably that there is no file called saxon9he.jar in the current working directory. Unfortunately Java doesn't give you an explicit error message for this, it just ignores this part of the command. Probably the current working directory isn't what you think it is. If you do "ls" or "dir" immediately before the "java" command, it will tell you what files are in the current working directory, which should include the saxon9he.jar file. If the JAR file is in some other directory, you can supply an explicit path, e.g. -cp c:/mike/java/saxon9he.jar.

Grails: how to define configuration profile when launching grails war with java command?

I have a grails 3.0 application packaged as war file, and I would like to run it with java command line. Things looks simple from the reference guide: java -jar my_war_file_name. But I cannot find a way to pass in the profile (dev/test/prod), and also it looks like -D option cannot overwrite the configuration as well. Any idea?
Best Regards
Sorry, I found the problem. I should put -D in front of -jar. So this is working:
java -Dgrails.env=prod -Dserver.port=xxxx -jar my_war_file

Solr : Can post.jar be used to post files recursively within folder and subfolder

If I would like to post all the xml files in a folder then I use post.jar.
java -jar post.jar *.xml
In case if I would like to post the files recursively ( i.e post xml files under subfolder level also is there anyway to achieve this.)
If you're on a unix-like (OSX or Linux), you can do something like this:
find . -name \*xml | xargs java -jar post.jar
That'll find all .xml files in or under the current directory ('.') and pass them as parameters to the java -jar post.jar command.
find is incredibly opaque, but very useful for stuff like this.
Please think twice before using stuff under the /example directory of Solr. I use Solr from Tomcat (instead of the Jetty embedded in the start.jar) and I use URLLIB2 in Python for POSTing data to Solr. (Jetty is a production-level software, so dont worry too much about that).
So, for uploading files, consider writing it in your favorite programming language. You can implement folder recursion yourself. For POSTing files, you need libCURL , which can send HTTP GET, form POSTs, multipart POSTs etc. A C program using libCURL needs no more than 8 lines to POST a file. CURL bindings exist for all major languages, so you can recycle libCURL stuff written in C to PHP, for example.
Yes, you can!
You just need to ad the -Drecursive flag.
java -Drecursive -jar post.jar *
For further options for the flags and usage just use:
java -jar post.jar -h

Can I specify the JDK path to compile against within an Ant build.xml?

I would like to use JDK 1.6 for a branch of a project while others keep using JDK 1.5. Developers want to occasionally switch between those.
So what is the best way to tell Ant's javac which JDK to use? By best, I mean a robust, transparent, low maintenance, versioned together with the source (Ant itself and JDK are certainly not, but they live at standard places).
The obvious -rather than best- way I guess would be outside of Ant: keep changing the JAVA_HOME env variable. However this would require developers to manually switch (another thing to remember: error prone), an changing all the -many- build servers (more work for me now).
Looking for some simple javac attribute e.g jdk-path, I noticed several instead (thanks to reading up on the net and in SO):
compiler- fair enough, but docs says "modern: .. javac1.5 and javac1.6 .. as aliases".. To me this suggests it won't make in any difference - will it?
source- seems only related to JLS version (althought not %100 clear from the docs linked above)
target - bytecode version
bootclasspath - some SO answers mention this, but pretty unclear and seems hackish
executable - path to javac, but not to libs.. -- seems the closest match, implicitly specifying JDK path? UPDATE: confirmed by JB Nizet
fork - it seems I'll need true here (otherwise it'll just ignore the above without error?). UPDATE: Any performance implications vs. default? (I guess JVM startup times are better these days, but still)
So, it seems none of these help in itself.. is any combination of these equivalent to setting JAVA_HOME prior to running Ant?
I have some hacks in mind (eg wrapping ant executable on each platform just to set that env var - quite sad), but I really hope I missed something :)
Using the executable attribute needs to set the fork attribute to true. This means that the javac ant task will launch an external process to execute javac.
When you launch javac manually, you don't have to specify any specific JDK lib directory: it knows where to find the libraries of the JDK it's part of. I'd say it will be the same if you launch it through ant's javac task (unless you override the bootclasspath).
Which version of the JDK is used to compile the classes should not necessarily matter. There may be differences in how a particular JDK compiles resources, but if the difference is just between v1.5 and v1.6, you can compile code to be compatible with Java 1.5 (or even 1.1) using a 1.6 JDK.
You can control what JVM version to compile for using the target attribute:
<javac srcdir="${src}"
destdir="${build}"
fork="true"
source="1.5"
target="1.6" />

Resources