First, I did look through the list of suggested questions generated by the keywords in my question but did not find anything relevant or helpful.
New to Java programming (not new to programming) so I don't know what useful tools there might be out there. I have a java console app, written using the intellij IDE. After testing and debugging, I am ready to deploy for a demo. Didn't find anything in the IDE that would let me do this!
I would now like to create a couple of installers - one for windows and one for linux. What do I do? I gather I just need the classfiles, but it would be nice to create an icon which would call the app with the right commandline options for the java.exe. As well, I have dependencies on log4j and jnetpcap (.dll requirements there)... how do I handle getting those support libs deployed - can I use the same installer or do I install them separately?
First things first, you need to JAR those class files. This is the standard way to package files in Java. A typical command would be:
jar cvf MyApp.jar *.class
Next you need to add a manifest to the JAR indicating the entry point into your program. Create a file called manifest.txt and add this line:
Main-Class: MyApp
MyApp would refer to the class name that contains the main() method. Now make the JAR again, this time specifying the manifest:
jar cvfm MyApp.jar manifest.txt *.class
On Windows, you can look into using Launch4J. You can use it to wrap your JAR in a EXE and specify that it runs as a non-GUI, console app.
In Linux, you can include a shell script along with your JAR to execute it. Place the script in your path. For example:
#!/bin/bash
java -jar MyApp.jar
It would be additional work to add dependent libraries to the mix as well as create installers. Seems too broad to include all in one question, but hopefully this will get you started.
Related
I need to start a Java application using a .bat file . Here is what I have so far, and it works fine
java -cp ".;C:\someLibrary.jar;C:\someLibrary.jar;..." Main
The problem is there are too many external libraries, and Eclipse already generates a .classpath file referring to all those libraries. Is there any way I could use that Eclipse generated .classpath file in my batch, so that I don't have to list all the libraries in the java command above ?
Can I use something like this
java -cp ".;C:\ ..\pathToEclipseFolder.classpath" Main
The reason I am asking this is because I will eventually end up updating some of those external libraries. And I want to still be able to use the original .bat file
If your Java application needs the libraries, then you must specify them in the classpath of your .bat file.
A couple of considerations:
1) Maybe you have more libraries listed in your Eclipse classpath than you actually need
2) Java6 and higher allows you to specify a directory, instead of requiring you to specify each individual library in that directory:
java -classpath ".;c:\mylib\*" MyApp
I have some code in Java using Eclipse and I would like to deploy it to unix envirnment. The program is simple console program that just takes some arguments at the run time, read a file and print out some results.
My question that what is best approach to deploy and run it in unix envirnment. I was just thinking to copy all the classes file to the unix envirnment and create a batch file to run the main class file. Does this sound okay? Or, should I create a runnable jar file?
Also, where should i put the jar files that the prgram is referencing (in classpath)?
Thanks
I think an executable jar file will solve your purpose here.
You should be able to execute it as
java -jar <jarfilename> <arguement1> <arguement2> .... <arguementN>
You can execute the jar file from the current directory itself, just make sure your jar file has executable permissions.
chmod +x <jarfilename>
ls -la
Designing for easy deployment is important in my opinion.
In our case, there are some components:
store project in the source code management system (git). we break down source code as
the developing source code to dev branch
the stable source code to release branch
use build tool, such as ant or maven, and provide a deploy script in the project. (we will talk deploy script in 3.).
provide deploy script to:
fetch the latest stable source code in the build server
build to executable files in the build server (whatever you do)
send the package to the target server
launcher (close the old app and run the new app) in the target server (via remote ssh command)
Currently, you think how to package the java, but it is a simple thing just about building and runing. When you talk about deployment, make it as easy as possible. Each time we deploy just to invoke the release script.
PS. I don't like the executable jar. Using un-packaging jars and compiled class can be sending by rsync very efficiently.
It sounds Ok and will work for you. Just one fix: you are going to write shell script for unix, not batch file.
But you can do better. Typically java classes are packaged into jar file. Jar file is just a zip file with optional META-INF, directory, MANIFEST.MF and other stuff. So it is better to package your application into jar and then run it as: java -cp yourjar.jar YourMainClass.
To create jar file you can use any tool that can create zip or utility jar that is a part of your JDK. You can also create automatic build using ant, maven, gradle, bildr etc that will help you to package your application.
I would do the following:
create a dedicated directory for this program. Copy the dependent .jar files to that directory
write a (short) script that sets the classpath to point to these jars and then executes the main class
Given the above, it's largely a matter of style as to whether you create a runnable .jar or not. It'll be hidden from the user.
I'm suggesting a script because:
you can set regularly used JVM parameters easily (memory options etc.)
it's a pain (and hardly intuitive) to type java -jar {pathtojar} etc.
By copying the jars to a dedicated directory, you can then use different versions of jars for different scripts (e.g. you may have 2 programs that use two different versions of commons-lang)
You should also (probably) use this script to explicitly determine which version of Java you use to run the program with. As you install/upgrade you don't want to break your programs and the scripts can be configured to explicitly tie down this info.
I find this quickest of all:
First, create a jar, copy to unix server and change file permission just as dopplesoldner mentioned below.
You can put your library classes and or jar dependencies in a lib folder
Then execute the jar
java -Djava.ext.dirs=lib/ -classpath yourJar.jar com.yourPackage.yourClass
yourClass will be the class having main(String args[]) method you wanted to execute.
I'm new to Java and aren't sure where to place the Java Dependencies which are required to run crawler4j. Do I put them in the same folder, or do I put them where Java is located on my machine, or what? Please help me.
Putting the dependent JARs in the same folder as your application JAR / bytecode files is a reasonable approach. As others mention, you need to ensure that the actual folder containing the JARs is on the classpath when the JVM is launched to run the application. The -cp argument is the recommended way to do this, and it is common practice to create a little shell script / batch file to launch the app with the appropriate JVM parameters.
Putting them into the Java installation is not a good idea for a couple of reasons.
It might have unforeseen side-effects on other applications run using that installation. This includes applications run by other users.
It will make upgrading your Java installation to the next patch level more difficult.
You need to put them on the CLASSPATH. If you're running your/the application from the command line you can specify your classpath using the -cp argument for java
You put them in your classpath. The classpath can be specified with the -cp argument when you run the java program.
java -cp depend1.jar;depend2.jar;etc... Class2Run
I have a java application that I created. Now I want to give it to my friend? How do I create a deploy version of this that they can run?
The easiest for you is to export the program as a "Runnable JAR file". Your friend then does this from the command line:
java -jar MyApp.jar
Probably the easiest way for your friend is if you take the above jar file and deploy it with WebStart. It's cross platform. Your friend simply uses their web browser to browse to the web page where the application is hosted and clicks on a link.
Create a JAR file (it's in the export menu), and send that to them. JAR files contain all the classes in a Java application, and a manifest that specifies things like which one is the main class. If the application depends on other 3rdparty libraries, you'll need to include them as well; it's possible to bundle them in the JAR file, but it's non-trivial
I think this is what you are looking for. Basically make it into a self-executing jar and run. It gets more complicated if you have other dependencies besides the standard API in Java.
Create a jar file, and put the following info in your manifest file
Manifest-Version: 1.0
Class-Path: lib.jar
Main-Class: mypackage.MyClassWithMainMethod
I have created a Java console application using Netbeans. In the Netbeans dist directory I have the class file of the project. Now I need to give the executable files to someone else
who will run them on another PC.
Which file I should send? How can he run them on his PC? Is there any way to create an exe type file?
Both PCs have the JDK installed.
Build a jar file with a main class specified in it.
If he has Java installed and .jar is associated with that, he should be able to just double-click on it.
Alternatively from a command line he'd be able to run:
java -jar program.jar
There are programs around to create executable wrappers around this, but a jar file is a simpler solution in terms of packaging - it's worth trying that to start with.
In additon to Jons answer:
If you have a runnable jar to start with, it is frequently much easier to package it up in an EXE file. If you have the need search Stackoverflow for JSmooth and one-jar.
NetBeans actually answers your question. If I do a Clean/Build, the output says:
To run this application from the command line without Ant, try:
java -jar "insert_your_project_name_here.jar"
theres a handy page about this here:
java tools tutorial
It describes creating jar files a little further down the page
You can go to run menu and select clean and build project or shift+f11 after this go to dist folder in project folder and use .jar file and run that by hint say in over .