I have a Maven application that includes .bat files, for usage in Window's CLI.
I'm considering having my .exe export (using launch4j) to request admin permissions and copy the resources to a subfolder in Program Files, is this good practice or should I move them somewhere else? If it is, how could I add the folder's /bin path to the PATH environmental variable?
For more clarification, I want to do this with java.
Did you try the maven exec plugin?
You can potentially execute any command using the exec plugin but specifically if you want to invoke your own Java program which is the output of your build, you can use the exec:java target. Instead of executing your jar, you can directly mention the main class of your application and also furnish it with vm arguments.
Detailed documentation is here
Please note that you can include the environment variables while invokign the exec target with the help of maven's own environment property access methods.
There is a lot more you can do with this plugin (create different run-modes using the configurations, run servers from within your project dependencies, redirect output to an error file (redirect the mvn output itself) and so on... Read the plugin documentation and try them all out..
Related
I have written a small application to parse a large XML file using SAX with Intellij.
I pass -DentityExpansionLimit=0 option to my application by going to Run\Edit Configurations... and set VM options.
It works perfectly when I run the application with Intellij, but when I create the artifact with intellij it doesn't work and I get the error which needed to set that option. This is obvious that the option didn't pass to the created jar file.
How should I achieve this goal?
Is there any command that I create with a batch file or something to set this option for my user? Is there any setting file that I can modify to set this option for my machine? (I use windows 10)
Usually, to send system properties to a jar, the command is something like that:
java -DentityExpansionLimit=0 -jar thejar.jar
You are mixing up two things here:
the JVM command line command, and the fact that you can pass arguments to your application, or properties to the JVM itself
your deployment artefact (probably a JAR file)
Meaning: It seems like you want to either pass command line arguments (to some main function) or properties to your application. But the JAR file doesn't have support for that.
JAR files are just a container of class files. You can add some META information via the manifest (which class to run), but that is about it. You can't magically push your IntelliJ "runtime configuration settings" into the JAR.
In other words: IntelliJ has no way of putting these values into your JAR.
When you invoke java -jar Your.jar ... then you (or some other tooling) has to add the required values to the command line.
I have created one java application which takes number of external jar files and also VM arguments passed to it.
I want to create .sh file for that application so that I cat run it on any linux system.
Please suggest me any tool to create .sh file in linux and which will also takes care about the arguments which has to be pass to application to run it.
I have use the tool named JarSplice but its not working as there is problem in loading libraries after creation of sh file .
So please suggest any tool for that.
If you're using maven to build your application there is a plugin called appassembler-maven-plugin that can create a .sh file for your application.
The groupId is org.codehaus.mojo.
You need to generate an executable jar, then you can simply run "java -jar main.jar" from there.
There are many questions on stackoverflow on how to create executable jars (you need ot set stuff in the MANIFEST.MF file in the jar file), for instance:
How do I create executable Java program?
I've built a JAR file and it executes fine on my PC (XP) which has Eclipse installed. It also works on another PC, which also has Eclipse.
I've tried running it on another PC(XP) that does not have Eclipse. Though it contains the JDK and multiple JRE. The JAR file just does not execute by clicking or from the command prompt.
I am not entirely sure, but my best guess is the Environment Variables are not set properly. Here is the error I receive from the command prompt:
Exception in thread "main" java.lang.NoClassDefFoundError: ...
Any help would be appreciated.
It must be a CLASSPATH issue.
The stacktrace should also say which class it failed to find. Once you have that, then find which jar has that class. Then add that jar file to your classpath or add it to the classpath env variable.
This is likely a classpath issue as others have said.
One thing to note is how your jar is constructed. You have a number of options in the dialog for exporting a runnable jar;
Extract classes into jar
Zip dependencies into the jar - creates jar-in-jar-loader.jar inside the jar.
Place jars in a subdirectory next to the jar.
Depending on what you have chosen for this depends on how the jar will behave. If the classes are extracted, dependent classes not in the JDK should be on the classpath. I'd recommend this course of action as it is simpler.
Now, the question is - are you using a dependency on your classpath not in the build dependencies of the eclipse project? If so, it won't be packed with / zipped into / put next to the jar because eclipse doesn't know about it (but java will still find it on your system because it's on the classpath). Also, if you've saved an ANT script and updated the build path in eclipse, eclipse won't update that ANT script - that is generated once only.
Environment variables are not considered when invoking a jar file when clicking on it (equivalent to running javaw -jar your.jar).
I'm pretty sure that it doesn't work on your first PC outside of Eclipse either.
When I finished to write my classes I put them into a package structure and then I jarred all.
Now which is the best way to deploy and use my jar?
setting classpath;
use CLASSPATH variable;
using the extension mechanism.
Don't update the user's CLASSPATH environment variable because there is a risk that your deployed application will interfere with other Java applications that the user might want to run.
Don't deploy using the Extension mechanism because there is a risk that you will interfere with applications run by any user using the JVM that you have "extended".
The best solution is to create and deploy a wrapper script that uses the "-cp" argument, or a shortcut that runs a self-launching JAR file ... as suggested by other answers.
I you have a main class in that jar that you want to run the best approach is to put a META-INF/MANIFEST.MF file in the jar and launch your class using java -jar mypackage.jar
The manifest should contain a Class-Path and a Main-Class attribute
See http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#JAR%20Manifest
If you mean by 'deploy and use' that you want to use it in another application, set it on the classpath of that application.
If you use the extension mechanism (I assume you mean putting the jar in pathtojava/lib/ext) every application using that jvm will have the jar loaded, the samecounts for the CLASSPATH as system variable. That is most likely not necessary?
If you ment to execute/use the jar as standalone application; just run it commandline, no classpath stuff needed. If you want to deploy on a server you probably wanted to make a war or ear fiel instead.
Best to my opinon is to provide batch files and shell scripts to start the application.
Sophisticated scripts check for environment variables like $JAVA_HOME (%JAVA_HOME%) and use them, if defined or use a default value.
Inside the script you could build the classpath in an internal variable and start the app with a line like
%JAVA_HOME%\bin\java.exe -cp %LIBRARIES% com.example.Main
I would prefer this solution over the java -jar Application.jar alternative, because this one requires that you setup the classpath inside the jars manifest. So deploying an application that depends on existing libraries on the target system is pretty difficults, just because you have to know the library paths before you build the application.
Setting the classpath (using -cp) is the best way, as you can have a different classpath for each application. The CLASSPATH environment variable is global for all Java applications running on the machine, as is the extension mechanism so you probably don't want to use those.
If your code is executable (ie it has a public static void main(String[] args) method) then you can also specify the class that the main method is in, and the associated classpath within the manifest file inside the Jar file. Once you have done that you can do this:
java -jar myJar.jar
And Java will work out the rest.
I've looked through many of the existing threads about this error, but still no luck. I'm not even trying to package a jar or use any third-party packaging tools. I'm simply running from within Eclipse (works great) and then trying to run the exact same app from the command line, in the same location it's built to (getting this error). My goal is to be able to zip up the bin folder and send it off to be run by someone else via a command line script. Some details:
It's a command-line app and I'm using the commons-lang-2.4.jar for string utilities. That is the file that cannot be located (specificaly "java.lang.NoClassDefFoundError: org/apache/commons/lang/StringEscapeUtils")
I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path"
The .classpath file looks correct and contains the reference to the jar, but I assume that file is only used by Eclipse (contains this line: <classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>)
Could this be related to the Eclipse working directory setting? I have some internal template files that I created that are under src/templates, and the only way I can seem to get those to be seen is by setting the project working directory to AppName/src. Maybe I should be putting those somewhere else?
Let me know if any additional info would help. Surely this is something simple, but I've wasted too much time on it at this point. This is reminding me why I originally left Java back in '05 or so...
A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.
In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.
The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:
java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
To save the effort of typing and editing the argument in commandline everytime, use a .bat file.
Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.
java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
Are you specifying the classpath to java on the command line?
$ java -cp lib/commons-lang-2.4.jar your.main.Class
The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.
If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:
java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar
If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).
Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with
java -jar yourProgram.jar
There are several variants depending on your needs.
Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See #BalusC's answer for good coverage of that.
Eclipse doesn't build executable java classes by default. Don't ask me why, but it probably has something to do with using their own tools.jar (somewhere in plugins/org.eclipse.core ?) so that Eclipse can run without a JDK.
You can usually go to your project bin directory and do:
java -cp . MyClass
But if you have external jars, Eclipse handles those internally in another weird way, so you'll need to add those too.
make sure your jar commons-lang-2.4.jar in classpath and not redudance.
I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth