How to export Java app as runnable? - java

I created a Java project called TotalBeginner, and exported as a jar. I then reference it in a desktop app with a SWT GUI, called MyLibrary. I now want to be able to run MyLibrary outside of the Eclipse IDE (I am running Luna 4.4.0). In following the advice of other answers to questions on Stack Overflow, I export as Runnable JAR File. I pick "Package required libraries into generated JAR" - so if I understand correctly, referenced libraries like TotalBeginner.jar should be included in the MyLibrary.jar, correct? However, when I run it, it returns to the command prompt with absolutely nothing appearing to happen. Task Manager (Windows 7) shows no Javaw process. What am I missing? Thanks.
C:\Users\jimerman\>javaw -jar MyLibrary-app.jar
C:\Users\jimerman\>_
No errors, no dialogs.

I suspect in your JAR you only have classes of your own project (which is fine in fact) and you haven't put all dependent JARs in classpath (As it is complaining for unable to find org/eclipse/swt/events/DisposeListener)
It may be tedious to find out all dependent JARs and put it in classpath of java command manually.
Consider making use of build tools like Maven and Gradle, which will save you trouble in collecting dependencies, and there are plugins for them to help you to construct artifacts that makes execution easier.
For example by using Maven, what you need is to prepare a POM, put SWT (and other dependencies) as dependencies of your project.
Then by making use of shade, appassembler or assembly plugins, you can easily have a uber-jar that contains all dependencies, or have a zip files that all dependencies are put in a specific directory and you can easily execute using generated command.

Related

Adding plain Java project as a classpath to an eclipse plugin

I have a plain Java project (not a plugin project) which I want to add to a classpath of a eclipse plugin which I am developing. But in web projects I can add that project as a build path and it works fine. But I tried same thing in eclipse plugin, I am able to compile successfully, but at run time I am getting java.lang.ClassNotFoundException.
I know OSGi quite well and I know how to add OSGi into an classpath (using export-packages) but what I want is to add Standard, non-osgi project into an classpath, so that I wont' get runtime errors. Is there anyway I can achieve this?
I can export project as a jar file or make it as a plugin project and it would work fine. But that's not my option currently because, still that API is in pre-alpha stage, and there would be lot of changes going on. So I am trying to avoid pain of exporting it as jar file everytime. Is there any option for me other than this?
I have a similar situation: I want non-OSGi Maven dependencies integrated into the classpath of my plugin. I succeeded with a roundabout solution, which I think is the best I could get.
I have a build step outside of Eclipse where I copy the class files of the dependency into the plugin's lib folder. The lib folder is specified in MANIFEST.MF as an entry in Bundle-ClassPath and (here comes the hack) as a source folder in build.properties. That was the only way to make the plugin work both when launched from within Eclipse and when exported.

package all external classes in my jar, with Eclipse

I am working on a Hadoop project in Eclipse that depends on another one of my projects; I've included the other project in my build path, but when I export the dependent project, it only contains the classes from that same project.
Ordinarily, this would not be a problem, as I could just link the other project with the -cp flag, but Hadoop requires you to pass the jar as an argument, meaning that all of my dependencies must be inside that jar.
Is there a way, in Eclipse, to automatically build and include classes from projects that you depend on?
Thanks.
You coud use Ant to automatically build, test and export. It needs some time learning it, but its worth.
There are possible tasks (fileset, zipgroupfileset, copy) to include files, jars (unzipped) or anything into the final jar. By this way you definitly know whats inside your distribution jar and you don't need an eclipe installation running.
I suggest you take a look at maven as a build tool. You define the dependencies and build steps for each of your projects in files called pom files. The maven plugins for Eclipse (the m2e plugins) can take the configuration in the pom file and setup your Eclipse build paths and project description so that you can access the classes in your other project in Eclipse. Maven can also create a jar for you that has the classes from both projects (a "jar-with-dependencies").
In maven terms, your two projects are called "artifacts" with one having a dependency on the other.
The one downside to maven (and the cause for many negative comments about maven) is an initially steep learning curve that can be frustrating. What you're trying to do, however, is very straightforward and I expect you can find a number of examples showing you exactly what you want to do.
The first step, and that's what my answer is about, is to take a look at maven. It may seem overly complex, but it can scale to handle just about any build configuration you need as your hadoop apps get more and more complex.
You can export a project as a Runnable jar, which can be useful if you want a single jar, with dependencies included.
Select the Project. File > Export. Select the Java section. Select Runnable JAR file.
See related answer:
Eclipse: How to build an executable jar with external jar?

Adding an external JAR to Eclipse Application debug configuration

The issue is as follows:
I have a 3rd party Eclipse plug-in installed and running
The above Eclipse plug-in provides an "extension point" of sorts - a source for a class I can extend/rewrite and replace in the plug-in lib directory (I am not saying it's a great idea, but necessity makes us do things we are not proud of - in-house development has its quirks)
The idea here is to envelop any such change in an external JAR (created by us) so that any change in code wouldn't require us to restart Eclipse to "refresh" the "extension point" class during development.
Eventually, a plugin/bundle will be created and placed as a dependency on the original plugin - so that installing and using that plug-in would actually, you know, work.
Question is - can I somehow add what is basically an external JAR file to Eclipse Application debug/run configuration?
Normally, adding JAR to classpath tab would work for regular Java application debugging/running. Eclipse Application however, doesn't have that tab.
So far the efforts (failed, perhaps due to some stupid syntax error or whatnot) included:
Adding the JAR as a dependency to Bundle-ClassPath: of the plug-in as C:\test.jar
Adding the JAR to the system's CLASSPATH variable
None of that made any difference - the code in the "extension point" that refers to a class in the JAR file fails - exception that boils down to:
Caused by: java.lang.ClassNotFoundException: test.Test
at org.eclipse.osgi.internal.loader.BundleLoader.
findClassInternal(BundleLoader.java:506)
Any pointer/help/criticism would be greatly appreciated.
Update
Apparently putting the JAR in the lib directory of the plug-in and updating the MANIFEST file doesn't work (not that it would help me - since such a solution would require a "refresh", which I am trying to avoid in the first place), so I must be doing something fundamentally wrong.
Does that make any sense or did I miss something in one of those attempts?
I have included an external JAR file to my Eclipse application.....not to the debug/run configurations but to the application itself.
What we did was to wrap the plugin into it's own java project. We are using maven for our build, so the pom.xml lists the dependency as the .jar file (which we put into a lib directory in the project). The manifest.mf file exports the needed classes. My application plugins have a dependency on this project. When I debug/run the application in the workbench I just make sure that this library project is included.
I'm not sure this answers your question completely or not....but I hope it helps.
A first guess based on some assumptions:
You are developing an extension to an 3rd party extension point 'foo'. Your application is a Eclipse Application, that you start via a run configuration. You need to create a new plugin 'foo-extension' which implements the extension point. You have to add the new plugin as a plugin dependency to your application. This way it will be available in the project class path, as well as in the 'Plugin-Ins' tab of your run configuration. Select the new project and when you start your application the extension will be available with the latest change.

Avoid ant and build automatically instead via eclipse.Is it possible?

I have let's say 4 eclipse projects.
Each references some other until one contains the actual main application.
Meaning that the idea is that each eclipse project is meant to be a library for a main application.
Each eclipse project references some other libraries e.g. spring, commons etc.
My question is the following:
I could write I guess some ant script that copies all the jars to a directory and builds the projects until we get the deliverable that has the classpath configured but is it possible to do something like this automatically via Eclipse?
I tried for example to do export as runnable jar and did not work.
All the referenced jar was not exported.
So is there some automatic way via eclipse or is ant the only option?
UPDATE:
I have no problem running the application inside Eclipse.The main eclipse project has references to the other projects it needs. My problem is the delivery of the application.
I.e. some runnable jar that runs standalone
Yes, you can do this, and you don't have to export any jars. When you modify the build path in eclipse (Build Path --> Properties), there is a section for referenced projects. Add the projects you need to reference to the main project and eclipse will treat them as dependencies.

Is there a way to export a Makefile from a Java Eclipse project?

So I have this Java project made up of several classes, some external JAR files and an executable Java program. I would like to export the whole code and external JARS to an external directory and to produce a Makefile to build the program with all the dependencies. Is there an automated way to do it?
Thank you
Tunnuz
I think I understand the question. Of course if you use an external build system like maven or ant, then we are decoupling the build process from the IDE. (But in some cases the IDE does integrate pretty closely with the build tool.)
But if you want to continue building using eclipse and to generate an ant file one fine day, then there is a tool for that. Its called EBuild. It leverages all the classpath information that eclipse already has and builds an generic ant file out of it.
Do you use maven?
If so this can be easily achieved with maven assembly.
If not, you can use ant to bundle exactly what you need.
When you right-click your project in Eclipse, there is an option called "Export". It can create build.xml for ant for your project.

Categories

Resources