I have a Java program in Eclipse on Mac currently, and I normally have to use multiple clicks just to export my code into a .jar file to test on my server. I would like to automate the process via terminal.
Basically, I compile my code usually by selecting the project
Export as Runnable JAR file
Select library handling: extract required libarires into generated JAR
Select export destination and hit done.
How can I do this via terminal? I assume this would first require me to compile the Java file, then to convert it to jar is a whole another step.
Help would be much appreciated.
You can create shell script that does it. This technique is obsolete since ~1998. So, use one of popular build tools. If you are starting now take a look on Gradle. Although there are a lot of other tools: good old ant, maven, buildr, ivy etc.
You can script all these activities using a build script. Several libraries exist for this, but Apache Ant is a good place to start. Ant build scripts can be run from command line or within eclipse, and will do all compilation, packaging and (some) deployment for you with a single command.
http://ant.apache.org/
Create default entry point manifest file as in : http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
Open Terminal and create an executable jar file like this:
Sample Script:
#!/bin/bash
# set CLASSPATH if needed
cd workspace/src
javac -d . *.java
jar tf exported.jar .
Related
Hi I wrote wordquiz program on Java. Using Eclipse in Unix.
In my linux machine it works fine.
Here is a source code https://github.com/HighlanderGe/Words
So, only basic package is used.
In windows compilation of such code as jar doesn't run. Neither in Mac.
As I guess problem is that in linux it is made to run from console, and console is comething very native for linux, but in Windows and I think in Mac too, cmd should be called, and from there it somehow to run.. but I bet cmd has no idea what is java. So some java console is needed for it?
The problem is not with the Mac or Windows, the problem is that you did not setup your workspace in Eclipse the same way on your different computers.
You can build your program on the command line in all environments in the same way. You just have to know the right steps.
First of all, there's an error in your code on line 25 in WordDatabase. Instead of:
dictionary = new ArrayList<>();
it should be:
dictionary = new ArrayList<String>();
After that, you can build your code like this:
javac -d . *.java
And run it like this:
java wordquizz/Wordquizz
This should work in any system that has Java, you just need to figure out how to setup your workspace in Eclipse the same way on your different computers.
UPDATE
I forked and converted your project to a Maven project:
https://github.com/janosgyerik/StackOverflow-Words
After you clone this to your PC, you can import into Eclipse using the File | Import... menu, and then Existing Maven projects option. It should work on all operating systems.
Maven is a recommended tool for building Java projects and it's a good thing to learn. After you install maven, you can build the project with:
mvn compile
You can package the project into a jar file with:
mvn package
You can run your code with either of these commands:
# needs 'mvn compile' first to generate classes
java -cp target/classes/ wordquizz.Wordquizz
# needs 'mvn package' first to generate the jar
java -cp target/wordquizz-1.0-SNAPSHOT.jar wordquizz.Wordquizz
If you like these improvements, merge from my repo soon. I won't keep it forever, I will delete it at some point.
UPDATE 2
To make a jar executable, you need to add inside a manifest file like this:
Main-Class: wordquizz.Wordquizz
You create the jar file with a command like this:
jar cvfm package.jar manifest.txt wordquizz/*.class
I updated my GitHub repository, so that now if you run mvn package, it will automatically add the right manifest, and the generated jar file will be executable.
If you installed java under windows you have to adjust the Path-variable, so the cmd knows where the java executable is. A good tutorial on how to set this up you can find here:
http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
After that you can simple go to the directory your source code is in and use the same commands as under linux to compile and run your application.
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 have been researching this for weeks and can't seem to get it figured out.
I have a Java program that I have written using NetBeans. It has several imports or .jar files it relies on. It runs fine in NetBeans. But I can't figure out how to call the .jar files and compile from the bash command line. I am using a Mac. I have read several posts on this and none so far have made sense to me. There are 26 imports being used in the program. I don't know if I need to use Ant or specify -CP or Classpath to compile. Surely I don't have to type each one of the .jar files out to compile this from the bash command line?
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
You should be able to simply use ant with the build.xml in the Netbeans project directory. It should "just work" for you. The project will likely not build WITHOUT Netbeans installed (if you tried moving the project to a different machine, for example), but with Netbeans, it should work out of the box.
If you don't have Ant installed, you'll need to install it.
Addenda:
To build it, if you have Ant installed, you should be able to simply go the project directory, where the build.xml file is, and type "ant", and it should build and put stuff in the dist directory.
If you go in to the dist directory and type java -jar yourapp.jar, it should run, because the manifest in the jar will point to the nearby lib jar files. If you want to distribute the app, there are different options for java, such as making a Mac compatible application, or a Windows EXE, you'll need to search for those. Or you can simply distribute the contents of the dist directory and write a script to do that whole java -jar yourapp.jar command.
If you are using Java 1.6 or above, you can toss all 26 jar files into a folder and simply add
-cp folder_name/*
as your classpath argument.
Some other options are 1.) type all 26 jar files on the command line (using the -cp argument as you have mentioned) 2.) use Ant or Maven or some other build tool and list those 26 jar files in the config file for said build tool or 3.) write a quick-and-dirty shell script that will set the CLASSPATH environment variable for you and then run your javac command.
So I have a java project in intellij right now. I'm trying to get it to run through ssh on a linux system.
I've uploaded the class files, however The project will not run unless I take out all the package declarations and rebuild the project.
Whats the correct way to build/run a java project using bash?
The only way to run a java program is to call the java command-line.
To build it is javac but if you hae uploaded the class files, it means that it is already compiled
make sure you have a entry main method in one of your java file, then use java xxxx -cp yourjarpaths to start your program. You can also use ant or maven to start your project, in bash, you can run "ant xxx" or "maven xxxx" to start the jvm
While you can use the command line to build and run the java programs using javac and java commands, you should consider using Maven or Ant or any other build manager for your project. This will be especially useful when you start having external dependencies in your projects.
I am Executing Shell scripts in Java Using Cygwin. My Scripts are running fine at command prompt, and even if I try simple shell scripts through Java it works fine.
But now my scripts are using another shell scripts from the folder called lib
So I need to include lib folder into my project.
Can any one suggest the way how to include shell script as a part of Java project.
Lib Folder-->
lib/lib.sh
lib/paxus.sh
lib/preload.sh
lib/tgzcreator.sh
lib/specific.sh
InSide Lib Folder these are the Shell scripts. Which my_script.sh is using.
Edit:- I want to add Shell script inside my project which uses Command line arguments and some other scripts from above folder structure.
cmd = "D:/cygwin/bin/bash -c '/bin/my_script.sh 121 121 1212 12121'";
Help me for Doing this.
If you are using maven as a build tool the best solution is to add scripts under /src/main/resources. In this case the scripts will be automatically included into your jar file, so you will be able to extract them on the fly from your java code and run.
If you want to have the scripts separately you suggested good solution. I mean lib folder. But again since I am using maven I'd put them under /src/main/sh (exactly as I put my java files under /src/main/java)