Am working to reproduce the "export runnable .jar file" functionality of Eclipse with command-line calls directly from the java program itself.
So those commands shall execute after Eclipse has compiled the project. I need this for better control what files are ending up in the .jar and to speed up the development process.
The following command successfully packs the structure and classes from the "bin" directory and also creates a manifest.mf file with the correct main class so the myjar.jar is runnable.
ProcessBuilder builder = new ProcessBuilder("jar", "cvfe", "myjar.jar", "main.MainControler", "-C", "bin", "/");
Process process = builder.start();
However my project has several libraries (.jar files) added to the classpath via the class-path dialogues of Eclipse. For example I'm using the pi4j library.
I expected that that its internal directory and class structure (something like "com.pi4j.***.class") would be added to the "bin" directory when eclipse compiles and executes the program.
However it is not there and therefore that never ends up in the "myjar.jar" either (and I get class-missing errors when running the .jar).
When I use Eclipse' export function, the files are there though.
What do I need to do to have the .class files from the additional .jar libraries exported so they can be added?
Huge thanks in advance :)
Your problem statement is not very clear and gives me the impression that you are trying to do something that should not be done.
First of all: if you don't want to use Eclipse to build the project then avoid using it to perform the first part of the work (i.e. compilation): you can perform the whole process from command line and this will give you even more control on the build process.
Second suggestion: do not reinvent the wheel. If you want to build from command line you can use Ant or Maven. In particular you can use Eclipse to generate an Ant build script that performs like "export runnable .jar file" then you can customize that script as you want: in Eclipse right-click on the project then select
Export / General / Ant Buildfiles
and you'll get a build.xml script to be used with Ant.
Related
I've written a few java applications (desktop, no browser involved) for friends, some of whom are less technical than I am. They use Windows, Linux and Macs. Originally I just used javac to generate bunches of .class files, zipped it all up and had them unpack it all. It works, but they had to install the JRE (terrifying for some), write a script to run java or javaw followed by command line args, app name, more arguments (incomprehensible for some), mark the script executable... I got mocked, somewhat rightfully, for a geek solution. They wanted to download from a website, doubleclick and be up and running.
I thought creating a .jar file would be the solution. It didn't help. In Eclipse, the options seem to be "create a jar file", which lets me include the handful of resources (.png files mostly) the apps needs, but the result isn't runnable from a command line. Or, create an executable .jar file, which doesn't seem (at least from Eclipse) to have a way to include resources - and the resulting .jar file doesn't start when double-clicked, even when I set the execute bit, even though the default run environment points to the Java suit. Even with a .jar file, I'm stuck with having them script "java -jar App...", and that's pretty much a dealbreaker.
I'm missing something. The point of Java is platform independence. Is there a platform independent way to have them download a single file, double click it and have it off and running?
If you are trying to create a runnable jar file from your project in Eclipse, you have to select that option when exporting. You can do this by Export > Java > Runnable Jar File. If you only select the Jar File option, you won't be able to run it.
If you want to wrap your application as a .exe, you can use launch4j. Here's a thread that explains it a little more and has some other options.
Sorry if this is a little confusing. I have a java file that uses a compilation command with another jar that I want to make into an executable(not .exe). I followed an online example to make a regular jar but get the NoClassDefFoundError due to the jar that it needs to run not being included. I am not sure what I either need to add to my manifest or the jar creating command to achieve this.
Thanks
I have not written java in a long time but try this,
Export(right click and select it from options) the project you're working on
Select runnable jar file.
Decide what you want to do with libraries
Finish.
NB: Try having the class files in the same location as the jar
I have an existing Java project that compiles and runs properly through Eclipse. I have created the following .bat file to run the program sans Eclipse:
java -classpath jflashplayer.jar;bin TestProgram
The file is saved within the project folder, but not within the bin folder (located in same directory as bin). When I try to run the batch, I am met with a large number of runtime errors, the first being
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils
I'm not sure why I get this error when it compiles and runs properly via Eclipse. I have the commons-io jar files linked to the project within Eclipse as libraries, and the jar files are themselves located in the project file (same directory as the batch file and the bin folder).
Also, I'm not entirely sure what the -classpath jflashplayer.jar bit of the batch file is doing. I am using the jflashlayer.jar library (also linked to the project within Eclipse and in the same location as the other jar files), but I am not sure why it would appear in the batch file. I edited an existing batch file from a similar project that uses the jflashplayer.jar files, and it has worked previously to leave that part in.
When I write code in Java, I rarely require it to compile/run outside of the IDE, so I usually have troubles when it comes to this part. Perhaps there is a more robust and foolproof method to run the program outside of the IDE other than the batch file method.
The batch file method is fine, but you have to specify all the libraries you're using on the classpath, just like the jflashplayer.jar.
In this case, the error you're getting is because the Apache commons-io library is not specified on the classpath. Your command would have to look something like:
java -classpath jflashplayer.jar;commons-io.jar;<other jars ...>;bin TestProgram
Alternatively, you can create a runnable jar from Eclipse as described here. When you select a library handling strategy, choose the option Extract required libraries into generated JAR. This will make it so that all the library classes you're using are packaged into your application's jar file, and you can just execute it by invoking java -jar my_application.jar.
I tried to run FindBugs in command line and had troubles when specifying the project to be analyzed. I understand FindBugs works on bytecode (.jar, .class), so I wrote a HelloWorld program and made sure that it had some messy code that would be detected by FindBugs.
Then I tried:
java -jar D:/findbugs-2.0.3/lib/findbugs.jar -project HelloWorld/bin
which threw an exception:
java.lang.IllegalArgumentException: Can't read project from HelloWorld/bin
at edu.umd.cs.findbugs.Project.readProject(Project.java:774)
I also tried .class and .jar files, but nothing showed up:
java -jar D:/findbugs-2.0.3/lib/findbugs.jar -project HelloWorld/bin/Main.class
java -jar D:/findbugs-2.0.3/lib/findbugs.jar -project HelloWorld.jar
I checked the FindBugs manual about the command line option "-project", it says
The project file you specify should be one that was created using the GUI interface. It will typically end in the extension .fb or .fbp
I don't understand this. Does it mean that some pre-processing is required and FindBugs cannot check arbitrary .jar or .class or project directly? How can I get this .fb or .fbp extension?
Thanks.
The procedure is described on the FindBugs website:
Make sure you download the FindBugs distribution which includes the GUI (called Swing interface).
Extract your downloaded ZIP and add its bin folder to your PATH.
Type findbugs to open the GUI, then click New Project
In the dialog:
Enter a project name, say HelloWorld.
Where it says Classpath for analysis, give it the Jar with your .class files or a directory where the .class files are (such as build/classes/main or whatever; the package structure must start in this directory).
Where it says Auxiliary classpath, list any libraries required to load your classes.
Source directories works just like Classpath for analysis, but for .java files. FindBugs uses this to show you where in the code your issues are.
You can select (cloud disabled) as bug store.
Click Analyze.
Now you can save the project configuration as a .fbp project file.
Next time, you can start the analysis by running
java -jar D:/findbugs-2.0.3/lib/findbugs.jar -project HelloWorld.fbp
If you don't want to or cannot use the GUI, you can get the text-only version by adding the -textui option as first option after findbugs.jar. Output formats and behavior are configured via additional command line options.
However, most people use FindBugs integrated with their IDEs, or as part of a build process. Neither use case should require the GUI or command line versions. Take a look at the plugins for your IDE, it may save you a lot of time and they are really easy to use.
I am working on eclipse, and I have the need to use external library's. For example Jsoup and JXL.
Now what I have done so far is: First created a "lib" folder in my project folder. Afterwards in eclipse, click on project properties, Libraries tab, add external jar and added the jar in the lib folder.
So this solve my compilation issue. Now, when I run the program (I go to project/bin and in the console execute: java ProgramName ; I get
java.lang.NoClassDefFoundError:
Now to testing, I added the Jar file to the folder where Main.java is and Now, I have been able to run the program doing the following:
javac -classpath ./path/to/jar Main.java
java -classpath ./path/to/jar:. Main
And this works.
So the first thing that comes to mind is that I have to tell java where to find the respective libraries. If this is correct? How do I do it?
java -cp ???(dont know what to put here)
But moreover. I have another issue. I am writing this program in a computer, but I am going to use it in other which probably don't have those libraries. How do I solve this issue?
I like to use something like the following:
java -cp myjar.jar;lib/*.jar com.foo.bar.MyClass
This adds not only my jar to the classpath but those in the lib directory as well.
If you want to run your jar on another computer, you will need those jars as well, you cant just have your jar. Why not just also package your lib directory along with it?
To get your program to run you have two paths to worry about
The path to the jar files that are your applications dependencies (like jsoup.jar) (lets call this lib)
The path to the directory containing the classes of your app (lets call this classes)
The general form of the command line you need is:
java -cp lib/jsoup.jar:classes Main
If you have more libs
java -cp lib/jsoup.jar:lib/jxl.jar:classes Main
A general note on packaging your app for release to other computers. You might want to consider making a jar of your own app, probably best done using http://ant.apache.org/manual/Tasks/jar.html
Another option is to produce a "one jar", which makes one large jar, bundling in all the classes you need from your libs and all the classes in your app. You can then make the jar executable for a nice out of the box solution. Have a look at http://one-jar.sourceforge.net/ and https://code.google.com/p/jarjar/
if you have this structure:
project folder
... code
... libs
then from the code folder:
javac -cp .;../libs/*.jar yourmainclass.java
java -cp .;../libs/*.jar yourmainclass
When you need to compile and run this project, take all the folder and do the same in other machine.