I have a Netbeans project that has a source folder (outside the Netbeans project folder). Now, When I compile the source code from the IDE, everything works fine. But, when I use my own build script it gives an error at runtime.
The application depends on several external libraries that I specify using the path element and the corresponding refid attribute in classpath tag (see code below)
Here is my buildfile
<project name="XX" default="dist" basedir=".">
<property name="dir.src" location="E:/XX git/xx/src"/>
<property name="nbproj" location="E:/Netbeans Project"/>
<property name="dir.dist" value="dist"/>
<path id="libs">
<fileset dir="${nbproj}/dist/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean">
<delete dir="dist"/>
<delete dir="release"/>
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="${dir.src}" destdir="build/classes" includeantruntime="false">
<classpath refid="libs"/>
</javac>
</target>
</project>
When I compile it using ant compile, it compiles all the source files without any error. But, when I execute it using the command
java -cp "E:\Netbeans Project\dist\lib\*;." controller.CZSaw
it creates the application GUI nicely and then when I perform some action, it fails with the following error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:167)
at controller.viewsynchronizer.CZViewManager.getIcon(Unknown Source)
at view.script.CZScriptView.<init>(Unknown Source)
at controller.script.CZScriptProcessor.<init>(Unknown Source)
at controller.script.CZScriptProcessor.getInstance(Unknown Source)
...
I know it would be difficult to point out some error looking just at the source code. But, as the same code compiled and worked nicely from the IDE, I think there is something wrong with the way I am compiling. Is there any apparent mistake in the buildfile.
Let me know if I missed some useful information here.
Classic mistake: assuming that because "everything worked fine" in one setting means that you've done everything right and are blameless for anything that subsequently goes wrong.
It means that your IDE took care of some things that you're ignorant of.
Look at the first class that's yours:
controller.viewsynchronizer.CZViewManager.getIcon(Unknown Source)
It looks like your controller is looking for an icon image that's not in the CLASSPATH.
Related
I need open this application-->
Decision tree
I have used netbeans and it has not worked, so I have installed eclipse and it seems that the file recognizes me.
What I do is import that application from Github, until there everything perfect, the problem is that when I give it to run, it does not work.
One problem I get is the following:
Buildfile: C:\Users\user1\workspace\Arbolito\java-decision-tree-master\build.xml
compile:
[javac] Compiling 9 source files to C:\Users\user1\workspace\Arbolito\java-decision-tree-master\build\classes
BUILD FAILED
C:\Users\user1\workspace\Arbolito\java-decision-tree-master\build.xml:19:
C:\Users\user1\workspace\Arbolito\java-decision-tree-master\lib
does not exist.
Total time: 550 milliseconds
I put the line of code in the XML file that gives me the problem
<target name="compile">
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" includeantruntime="false" debug="on" />
</target>
Does anyone know how to run the program?
Create a lib folder in your project and include SL4J and Junit jar in that folder.
I created a basic Ant build using nice tutorial on Apache website.
<project>
<target name="clean">
<delete dir="bin"/>
</target>
<target name="compile">
<mkdir dir="bin"/>
<javac srcdir="jar" destdir="bin"/>
</target>
<target name="jar">
<jar destfile="HelloWorld.jar" basedir="bin">
<manifest>
</manifest>
</jar>
</target>
</project>
This should match my folder structure:
But I get an error:
The archive: C:/Program%20Files%20(x86)/Java/jdk1.7.0_11/lib/tools.jar which is referenced by the classpath, does not exist.
The file exists on that path, but of course you must replace URL encoded characters %20%->.
I'm not sure if the URL encoded spaces cause the error though. Could anyone tell me what's going on with Ant? I'd like to use Ant instead of my ad-hoc batch-file build system.
I resolved the problem by setting environment variable JAVA_HOME to correct JDK (not JRE) path. This can be done in control panel or using utility programs.
I have create RESTful web service based on the JAX-RS and used Jersey embedded web server. My ant script compiles code successfully while it gives me error ClassNotFoundException when I run my main class. So after doing research I came up with solution & here it goes java build ant file with external jar files . What I did was created a bundled jar file try to execute that & it works perfectly fine. I want to know the reason behind :
why this solution works ?
Why I should combine all jar file ?
Is it similar to war file which we create following J2EE architecture otherwise war will not be extracted by server ( say TOMCAT ) & in my case jar file for Jersey embedded HTTP server?
EDIT:
Here is my ant build.xml file
<property name="lib.dir" value="${user.dir}/lib"/>
<property name="build.dir" value="${user.dir}/build"/>
<property name="build.lib.dir" value="${build.dir}/lib"/>
<property name="build.classes.dir" value="${build.dir}/classes"/>
<property name="src.dir" value="${user.dir}/src/main/java"/>
<property name="main.class" value="com.assignment.ConsoleServer"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="init" depends="clean">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes.dir}"/>
</target>
<target name="copy_jars" depends="init" >
<copy todir="${build.lib.dir}" >
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</copy>
</target>
<target name="compile" depends="copy_jars">
<javac srcdir="${src.dir}" destdir="${build.classes.dir}" classpathref="classpath" includeantruntime="false"/>
</target>
<target name="jar" depends="compile">
<jar destfile="${build.dir}/${ant.project.name}.jar" basedir="${build.classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
<zipgroupfileset dir="${lib.dir}" includes="*.jar"/>
</jar>
</target>
<target name="run" depends="jar">
<java fork="true" classname="${main.class}">
<classpath>
<path refid="classpath"/>
<path location="${build.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
Here is my folder structure
P.S. I am not java expert so pardon me if this question is stupid.
Why this solution works?
In your particular case, you probably didn't include all of the necessary dependencies in your deployment in your previous. (It is not clear from your question how you were originally doing the deployment.)
Now you have put all of the application and dependent class files, etc into one JAR file, and presumably you are deploying / running that file. It works because now it has everything that it needs to run ... which it didn't before.
Why I should combine all jar file?
In your case I suspect that it was not strictly necessary. There was probably a way to "deploy" all of the dependencies without combining them into a single JAR file.
However, there is one case where a "uber-jar" has advantages. That is when the JAR is intended to be an "executable" JAR, and you want to be able to distribute / install it as a single file. (And executable JAR
file can refer to external JARs, etc, but the way that you have to do
it is "fragile".)
Is it similar to war file ... ?
Sort of, though a WAR file contains JAR files ... and typically other kinds of resources that the web-container understands.
The solution works because you packed all you service classes and depending libraries in one jar. That jar and everything inside will be in the class path and visible to your execution virtual machines class loader.
If you leave your depending libraries out your Jersey Web server needs to have them on it's class path, then you wouldn't get ClassNotFoundExcpetion
You shouldn't pack web application in single jar. You should crate war file where you dependencies will be placed inside WEB-INF/lib. You would easily then deploy that war on any application server. Switching to Maven instead of Ant can help a lot.
EDIT: After you added more details to description and ant
If you don't want to use fat-jar you can either
modify your antjava task to specify classpath that will reference
all external libraries (basically telling ant how to build
-classpath parameter for java -jar command
even better, modify your javac ant task by making complete Manifest file that specifies Class-Path correctly, take a better
look at the solution (at the bottom) of the answer you linked (java build ant file with external jar files)
For completness reference on Manifest here
Good evening Stack Overflow!
I started learning Java quite a few days ago, but not using an IDE once my computer is a bit slow. So, I decided to use Sublime Text, and compile things using ant on the cmd, since it seems to be the most reasonable thing to do.
Today I started ( at least tried ) to follow along a series of LWJGL tutorials (which are really cool), from ThinMatrix, but I can't manage to solve an error which I get every time I try to compile the 'project'.
This is the structure of my project:
LWJGL
src
com
game
test
MainGameLoop.java
renderEngine
DisplayManager.java
build.xml
And ladies and gentlemen... the build.xml (following Ant's official HelloWorld tutorial):
<project name="LWJGL" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="lib.dir" value="lib"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="com.game.test.MainGameLoop"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar" />
</path>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" classpathref="classpath" />
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true">
<jvmarg value="-Djava.library.path=lib/natives-win" />
</java>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
Every time I run ant on the command line, inside the root folder of my project, I get this:
[java] Error: Could not find or load main class com.game.test.MainGameLoop
[java] Java Result: 1
I'm struggling to understand what is the root problem of this, I kinda sexually abused Google Search trying to find an answer on Java forums, Blogs, and even here...
I don't usually like populating Stack Overflow with noob questions, but I have to admit I don't know what to do.
Thanks in advance!
Your code depends on libraries. You have successfully added the libraries in the classpath to compile your code, and have thus successfully created an executable jar files containing your classes. But these classes depend on libraries to run. And when running the jar file, you don't secify anywhere that Java should look for classes in libraries in addition to your jar file.
See Generate manifest class-path from <classpath> in Ant for how to add a Class-Path entry to the manifest of your executbale jar file. Beware: the paths of the libraries must be relatie to the path of the jar.
Ant is a bit outdated. If I were you, I'd try using gradle, which has a neat application plugin doing all that for you, and much more.
I finally managed to solve this problem that I was struggling with by the help from a good friend of mine. There are two tricky points:
First, when specifying the "Main-Class" in the manifest section, just use the following format "packageName.className", and there is no need to specify the "build" or "source" folders!
Second, try and zip all the jar files that your "jar" file is going to depend on using "zipgroupfileset"
This image shows how I have commented out the "Class-Path" attribute that I would use for addressing the dependencies of the project, and have replaced it with the "zipgroupfileset". I hope that help you as well.
Small thing that can cause this issue is using uppercase letters in file extension. For example you can't use MainGameLoop.Java, The correct extension should be all lowercase like below.
MainGameLoop.java
Please check that first.
I'm completely new to Ant and need to add a couple jars to my classpath, compile a couple .java files, and run a junit test, all in Ant. I've been looking at a few online tutorials and manuals, but can't seem to wrap my head around the entire xml writing process.
All the previously-written code resides in a single directory called XXX.
In XXX there are two jars I need to add to my classpath with export CLASSPATH=$CLASSPATH:jar1:jar2, two java files I compile with javac *.java, one of which contains several junit tests that I run with java org.junit.runner.JUnitCore Tests. The build.xml would reside in XXX as well (I believe).
So far I have the following for just compiling, although I think there's a lot missing.
<?xml version="1.0"?>
<project name="EtlAutomation" default="compile" basedir=".">
<property name="src" value="${basedir}"/>
<target name="compile">
<!-- Compile .java files -->
<javac srcdir="${src}" destdir="${src}"/>
</target>
</project>
What else do I need to add to compile *.java in the current directory? How can I run the export CLASSPATH command, and finally the junit commend?
I'm not asking for anyone to write my code, but it would be appreciated. If anyone knows a good beginner tutorial for a unix environment, that would be awesome. I'm a total beginner with ant so I'll take what I can get.
Here is a previous question addressing this. And this may work for you:
<project name="EtlAutomation" default="compile" basedir=".">
<property name="src" value="${basedir}"/>
<path id="compile.classpath">
<fileset dir="./">
<include name="*.jar"/>
</fileset>
</path>
<target name="compile" >
<javac destdir="${src}" srcdir="${src}">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="run" depends="compile">
<junit>
<classpath refid="compile.classpath" />
<test name="TestExample" />
</junit>
</target>
</project>