views:

96

answers:

4

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.

A: 

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.

gmale
A: 

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.

CoolBeans
I didn't think I needed to set a classpath as the dependencies were all compiled in to the jar.
danpalmer
okay if you had them statically linked then you shouldn't have to set up classpath. I was not sure how you are running the command to execute your program. Looks like you were missing the -jar option.
CoolBeans
A: 

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
Sean Reilly
Thanks. I built the jar with Fat-JAR so al the dependencies were inside it. I guess that the -jar option tells java that this is the case. This works perfectly. Thank you!
danpalmer
This just shows how little I know about Java. The language seems easy, but getting it set up is the complicated bit.
danpalmer
A: 

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.

Miklos