How do I make java libraries visible to clojure? - java

Ultimately, I want the following to work
(ns grizzler.core
(:import (com.sun.grizzly.http.embed GrizzlyWebServer)
(com.sun.grizzly.tcp.http11 GrizzlyAdapter)))
However, I have no idea how to go about this. Do I add stuff to my classpath? Where do I modify my classpath, in my .bashrc or within clojure?
I've found the grizzly project at http://grizzly.java.net/. But what do I download? How do I install things? I really just have no idea what to do.
Related: Using 3rd party java libraries, like com.jcraft.jsch, with clojure - except that it's not detailed enough for me :(
Edit: I also tried the following in project.clj and it didn't work:
(defproject grizzler "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.3.0"]
[com.sun.grizzly.http.core "2.1.10"]
[com.sun.grizzly.http.embed "2.1.10"]
[com.sun.grizzly.tcp.http11 "2.1.10"]])
In addition, I tried net.java.grizzly.http.core , and that didn't work either.
Thanks!

The current consensus on the "right" way to do this is basically "use leiningen". Leiningen is basically a wrapper around maven and uses maven repositories. Most of the time you can find the code you need in a maven repository somewhere, though eventually you will be in this situation where you need to use a jar file. In this case you install the jar file into your local maven repository which lives in your home directory and contains copies of all the jars required to build all your projects. In cases where the jars you depend on are available from a central repo this local repo acts as a cache so you don't have to download them every time, though you can manually put jars into your local repo if it can't automatically download them.
I recommend watching this video first.
check carefully to see if the library you need is already available
download the jar you want
download leiningen
run lein new nameOfYourProject
add it to your leiningen project's project.clj
run lein deps
this will print the full command for installing the jar file to your local maven repo
run this command (as printed by leiningen) and your jar file should be copied to the correct location under ~/.m2/...
run lein deps again to make sure it finds it
add an import statement in your .clj file (as you have above)
success!
Debugging this process can be very situation specific, but the denizens of #clojure are usually quite helpful.

Related

Compiling external libraries for a terminal programs?

How can I compile terminal programs which uses external libraries? I'm using JColor (https://github.com/dialex/JColor) to color my font but I don't know how to compile it.
My IDE is IntelliJ. I already tried to compile my program into a jar but executing it in terminal ignores JColor. No error but no color and cryptic symbols instead. I don't know if it's cause of JColor or I missed something during compiling.
Thank you very much in advance.
Edit : The real issue
(See replies)
We found the issue not to be related to imports, but rather that ANSI control support is disabled within windows terminals by default, but enabled within IntelliJ.
This made it seem as though the library was not working after being exported, or wasn't being exported.
Relavent Discussion
The dependancy
InteliJ gives the option to export libraries with the module from within the Project Structure window:
Assuming the library is configured within your project, all you have to do is navigate to Project Structure > Modules > myModule > Dependencieswhere you can add the library, and tick export
The artifact
Next, create an artifact from your module, with depenencies
The output layout shows the internal layout of the jar after export, check the library is in there.
Now when you build your artifact, it should contain the dependency.
javac -cp /path/to/jar/file Design.java
java -cp .:/path/to/jar/file Design
So, lets suppose your current working directory in terminal is rc/structure/

Create Java projects without eclipse?

Is there any way I can create Java projects using a simple text editor? Not an IDE like eclipse?
I want to be able to create .jar files without the assistance of an IDE, I have all the JDK commands installed already on my computer (such as javac)
I'd like to know what file structure I need, how I need to arrange my class files, what compilation steps I need to go through etc. to be able to create jar files.
Yes, completely doable (just not much fun once the project gets bigger).
I suggest if it's not a throwaway project, use a build tool like Maven or Gradle to manage your build process, so that you don't need to assemble classpaths and resources yourself, but still retain full control of the build and test lifecycle, without IDEs. This comes at a complexity cost, of course, but once it's set up life becomes easier.
See also How can I create an executable JAR with dependencies using Maven? or the Gradle docs about creating JARs
I'd highly recommend the standard Maven source directory layout too (src/main, src/test etc) as it's both commonplace and makes for easy integration with the above tools.
Follow the below steps to create a jar file
Compile the java source using javac
Create a manifest file (if main exists) to identify main class
Use the below command to create a jar file
jar -cvfm *.class
Yeah. You can create your project structure without an IDE. But it's time consuming and you have to do everything.
To talk about creating JAR, you don't want any extra software. You can use jar utility, which comes with JDK.
Here are steps to create jar:
Compile classes which you want to in jar
Create manifest file (.mf). It's needed if you want to make jar as executable. If you want to bundle classes only together, then no need. (eg. Dependency jar)
Go to command prompt and execute following command "jar cvf MyJarName.jar *.class". Make sure java is set in environment path and you're inside the directory of classes.
cvf means "create a jar; show verbose output; specify the output jar file name.
That's all. If you want to include any folders inside jar then you can use folder name in above command after classes and it must be separated by space.
Example: jar cvf TicTacToe.jar TicTacToe.class audio images

Building .jar file with IntelliJ IDEA gives me a file that can't be used

I'm currently working on a project where I'm using the LWJGL library and I just tried building a .jar out of it all, now the problem is that nothing happens when I try to start the .jar.
My only thought is that maybe the library didn't export correctly but I've been looking around at the other questions here for a while and I've have added everything correctly at the Dependencies tab in the Project Structure, the libraries I need are also in the Artifact Output Layout as extracted directories.
I just tried checking the stacktrace with cmd and it says that it can't find lwjgl in the library path so I'm guessing there's something I don't know about .jar files or something that's causing this error.
Here's the stacktrace:
Also in IntelliJ I've put "-Djava.library.path=lib\native" in VM options, is it possible that this doesn't apply to the .jar or something.
Help is appreciated!
There is a rather complicated process to make an executable jar with LWJGL. You need to use Jarsplice.
Jarsplice has 4 steps which are pretty self explanatory but here they are anyway:
1: Add Jars.
Add the jar you generated as well as all the libraries. That includes lwjgl.jar, and any other jars you used.
2: Add Natives.
Add all the natives you used in your project. These are the files you referenced using -Djava.library.path.
3: Main Class
Select the main class of your program. This could be something like com.example.game.EntryPoint.
4: Create Fat Jar
Click "Create Fat Jar" to create your executable jar!
You can also optionally create a Windows .exe, OSX .app, and Linux .sh executables as well.

Configuring Cobertura with Ant

I'm trying to get Cobertura working for my project's Ant build, and all I have to work with are the documentation/FAQs and a sample build.xml that uses cobertura, but apparently never worked (ha!).
I see in the Cobertura taskdef a resource called tasks.properties - what is this file and where is it located on my machine (I couldn't find it anywhere inside my Eclipse home)?
Where am I supposed to install the cobertura.jar to, so that Ant knows how to reference it directly (instead of an absolute path)?
Thanks in advance for any help here.
tasks.properties is bundled inside the cobertura JAR file; you can view it like so on a UNIX-like system:
jar xf cobertura.jar tasks.properties; cat tasks.properties
You could put cobertura.jar in the lib subdirectory of your local Ant installation if you want to it always be available, but IMO it's better to store it in a separate location and add it to your classpath explicitly in your build file. This prevents unwanted classes from being loaded in other builds.

Deploying a Java application. How?

I am new to Java (and Eclipse) but I have used .NET (and Visual Studio) a fair amount. I also know about compiling C/C++ code and things like that. I know that at the end I get either an EXE or a nice binary file that can be run from the command line.
I have been making a Java utility that uses some external libraries. I need to compile this into an executable that I can run from the command line on a unix machine, but I cannot find any way to do this.
I can build and run/debug in Eclipse, but that is no use to me as this code will be run on a webserver. I just need all the dependancies compiled in to one file but after hours of searching on Google, the best thing I could find was the Fat-JAR plugin for Eclipse and after using that I just get the following error when I try to run the file:
Exception in thread "main" java.lang.NoClassDefFoundError: Network/jar
This is really confusing me and as it is such an essential thing to be able to do I am sure I must be missing something blindingly obvious, but as I said, after hours of searching I have gotten nowhere.
I really appreciate any help you can give me. Thanks.
If you build your java app using Maven (which can be done with every major IDE), then you can use the maven Shade Plugin to build a shaded jar file; this is a jar file with all of its dependencies included.
A shaded jar can be run from the command line like this:
java -jar myjar.jar command line options
You're doing something standard and you're using eclipse. This means, in your case, Maven is your friend. Download and install the M2Eclipse plug-in. Maven is best at managing dependencies. So, creating a jar with dependencies included will be very, very straight forward. There are thousands of examples on the web and in StackOverflow. If you have problems setting it up, comment on this and I can point you in the right direction.
Sounds like your class path on the server needs to be modified to pick up the jar file containing the Network class. How are you executing your program? What path(s) are you putting in the -cp option?
If you are not sure how to find out the contents inside a jar file, run jar tf , this will list the packaged classes. Validate that one of the jars in your CLASSPATH has that class it says missing.
Give us more details and we can help solve it.
I think I should first explain some basics. A Java class can be run as an application if it has a public static void main(String[] args) method. If it has this method, you can run it from command line as:
java my.package.MyClass <attributes>
Before launching your app, you need to make sure that all required .jar files (and the root of your own class folders, if you did not make a jar from your classes) are in the CLASSPATH environment variable.
These are the absolute basics. When you are building a more complex app, or an app for distribution, you'll probably use maven or ant or some other tool that makes your life easier.

Categories

Resources