Configuring Cobertura with Ant - java

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.

Related

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

How does Eclipse set Native.loadLibrary path?

I inherited a working large Java8 project that needs to be re-architected. It builds a jar, but also includes an example main (not included in the jar) and a deep tree of libs including some .dll files deep in the libs/ tree (dll's also NOT included in the jar).
At some point, the code in the jar calls Native.loadLibrary("fti2x.dll", fti2xWrapper.class, [Map< String, Integer>] options) on the simple file name "fti2x.dll" -- with no path components -- and the load succeeds. This .dll is only located deep in the libs/ directory on my computer. How did the jar, running under eclipse, figure out this was in /libs/client/usb/CommLib/amd64/fti2x.dll without specifying the path to it?
If I copy just the jar, the example main, and the libs/ directory to a new project, how do I configure it in Eclipse(Mars) to find the .dll for the Native.loadLibrary call? (Just simply doing so fails, so I am missing some config.)
How do I do execute the main.class outside of Eclipse and have it find the .dll?
Is there possibly some Java code I am missing that sets this path? If so, what do I look for?
There is a system property that is used to specify the path that JNA uses to search for native libraries: jna.library.path. Try printing out its value just after the library loads successfully in the old project to see if it is being set.
This answer explains how to set a system property in an Eclipse run configuration. Outside of Eclipse, specify a value for it using the -D command-line switch to the JVM, e.g.
java -Djna.library.path=<some path> YourMainClass

Adding comm.jar into my jar

I add comm.jar library into my .jar and I get javax.comm.NoSuchPortException.
Is this bug because I haven't add javax.comm.properties and win32com.dll into my jar? How can I add this file inside manifest?
Java does not support Jars within Jars. The correct way to ensure the javax.comm packages are on the run-time class-path of the application is to add a manifest that provides a relative path to the dependent Jar, and ensure that Jar is in the right place to be found. For details see Working with Manifest Files: The Basics & particularly Adding Classes to the JAR File's Classpath in the Java Tutorial.
There are other methods that can be used for applets and apps. launched using Java Web Start.
#Gogoo's answer says to copy stuff into your JVM installation directory.
It should work, but it is generally not a good idea:
The stuff that you install that way will be shared by all applications that you run using that installation. Depending on the what it is, it may interfere with other applications.
Each time you update your JVM, you have to remember to copy those files into the new installation directory.
IMO, a better idea is to put those files in a separate directory tree, and write a wrapper script to launch your application with the files on the classpath and library path. And/or see #Andrew Thompson's answer.
Try FatJar.
The Fat Jar Eclipse Plug-In is a Deployment-Tool which deploys an
Eclipse java-project into one executable jar.
It adds the Entry "Build Fat-JAR" to the Export-Wizard. In addition to
the eclipse standard jar-exporter referenced classes and jars are
included to the "Fat-Jar", so the resulting jar contains all needed
classes and can be executed directly with "java -jar", no classpath
has to be set, no additional jars have to be deployed.
copy comm.jar \jdk1.6\jre\lib\ext
copy win32com.dll \jdk1.6\bin
copy javax.comm.properties \jdk1.6\jre\lib
and start your.jar over command line:
java -jar your.jar

Unable to run JAR file on another PC. Possible issue with Environment Variables?

I've built a JAR file and it executes fine on my PC (XP) which has Eclipse installed. It also works on another PC, which also has Eclipse.
I've tried running it on another PC(XP) that does not have Eclipse. Though it contains the JDK and multiple JRE. The JAR file just does not execute by clicking or from the command prompt.
I am not entirely sure, but my best guess is the Environment Variables are not set properly. Here is the error I receive from the command prompt:
Exception in thread "main" java.lang.NoClassDefFoundError: ...
Any help would be appreciated.
It must be a CLASSPATH issue.
The stacktrace should also say which class it failed to find. Once you have that, then find which jar has that class. Then add that jar file to your classpath or add it to the classpath env variable.
This is likely a classpath issue as others have said.
One thing to note is how your jar is constructed. You have a number of options in the dialog for exporting a runnable jar;
Extract classes into jar
Zip dependencies into the jar - creates jar-in-jar-loader.jar inside the jar.
Place jars in a subdirectory next to the jar.
Depending on what you have chosen for this depends on how the jar will behave. If the classes are extracted, dependent classes not in the JDK should be on the classpath. I'd recommend this course of action as it is simpler.
Now, the question is - are you using a dependency on your classpath not in the build dependencies of the eclipse project? If so, it won't be packed with / zipped into / put next to the jar because eclipse doesn't know about it (but java will still find it on your system because it's on the classpath). Also, if you've saved an ANT script and updated the build path in eclipse, eclipse won't update that ANT script - that is generated once only.
Environment variables are not considered when invoking a jar file when clicking on it (equivalent to running javaw -jar your.jar).
I'm pretty sure that it doesn't work on your first PC outside of Eclipse either.

The dreaded java.lang.NoClassDefFoundError

I've looked through many of the existing threads about this error, but still no luck. I'm not even trying to package a jar or use any third-party packaging tools. I'm simply running from within Eclipse (works great) and then trying to run the exact same app from the command line, in the same location it's built to (getting this error). My goal is to be able to zip up the bin folder and send it off to be run by someone else via a command line script. Some details:
It's a command-line app and I'm using the commons-lang-2.4.jar for string utilities. That is the file that cannot be located (specificaly "java.lang.NoClassDefFoundError: org/apache/commons/lang/StringEscapeUtils")
I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path"
The .classpath file looks correct and contains the reference to the jar, but I assume that file is only used by Eclipse (contains this line: <classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>)
Could this be related to the Eclipse working directory setting? I have some internal template files that I created that are under src/templates, and the only way I can seem to get those to be seen is by setting the project working directory to AppName/src. Maybe I should be putting those somewhere else?
Let me know if any additional info would help. Surely this is something simple, but I've wasted too much time on it at this point. This is reminding me why I originally left Java back in '05 or so...
A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.
In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.
The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:
java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
To save the effort of typing and editing the argument in commandline everytime, use a .bat file.
Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.
java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
Are you specifying the classpath to java on the command line?
$ java -cp lib/commons-lang-2.4.jar your.main.Class
The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.
If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:
java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar
If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).
Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with
java -jar yourProgram.jar
There are several variants depending on your needs.
Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See #BalusC's answer for good coverage of that.
Eclipse doesn't build executable java classes by default. Don't ask me why, but it probably has something to do with using their own tools.jar (somewhere in plugins/org.eclipse.core ?) so that Eclipse can run without a JDK.
You can usually go to your project bin directory and do:
java -cp . MyClass
But if you have external jars, Eclipse handles those internally in another weird way, so you'll need to add those too.
make sure your jar commons-lang-2.4.jar in classpath and not redudance.
I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth

Categories

Resources