Compile NetBeans project from command line by using Ant - java

I have a NetBeans project I would like to compile from the command line. There are many other questions on StackOverflow about how to do so, but they explain how to compile the project using commands like javac src/*.java.
I haven't changed my NetBeans project's build settings. By default, how can I compile my project from the command line using Ant? Once I've built my project, where is the compiled file located, and what format is it in (i.e., .class files, one .jar file, etc.)?
(I understand that asking how to use Ant to compile my project in general is too broad of a question. That's why I'm specifically asking about how to compile using NetBean's default configuration for a project.)
I'm using NetBeans 8.0.2.

ant compile Compiles the project (.class files are placed in the build/classes folder)
ant jar Compiles the project (see above) and builds a JAR ( located in dist/ )
If that doesn't work for you, check ant's output for errors. (Is the JAVA_HOME Variable set properly?)

I'm totally agnostic IDE developer. After several frustrating years trying to emerge "netbeans ant config" to something usable from command line I became to create a wrapper for netbeans ant.
https://github.com/albfan/ant-netbeans
By now you can:
detect defined targets with standard
$ ant tabtab
Rely on project will honor JDK_HOME
and most important
Expect all ant target to complete smoothly, passing test and whatever stuff implied.

Related

How can I access IntelliJ's java compiler from the command line?

I have a very large maven project, and when I'm debugging, it sucks to have to rebuild the entire project just to see the changes. My current method is to compile the java file in IntelliJ, then copy the class file in my target directory, to my tomcat/webapps/project/ directory, and replace the existing class file with the intellij compiled class file. Then rebuild the war file, and replace it with the existing war file in the tomcat/webapps directory.
I want to be able to automate this process. I looked into doing it with the javac command, but I'm having problems with packages be imported from jar files in the .m2 directory, and can't figure out how to automate the classpath, depending on the specific file. I'm sure IntelliJ does this automatically, and was wondering if there's a way to run IntelliJ's compiler from the command line, or if anyone has any insight into how I can get javac to work for just the one file in my whole project scope.
Any help is appreciate, thanks!
IntelliJ delegates (by default, there are options to delegate to the Eclipse compiler or other ones) to the javac compiler.
If you do not want to import your project into IntelliJ, then you can use maven from the command line to build the project.
If you do not want to build the whole project, then you can use javac from the command line.
To generate your classpath, use :
mvn dependency:build-classpath -Dmdep.outputFile=classpath.txt
classpath.txt will contain your classpath.
From then you can do : javac -cp (contents of classpath.txt) your java file.
IntelliJ is fast even on large maven projects and it can easily compile into an exploded war your tomcat would point to. It is unclear to me on why you would not want to benefit from IntelliJ.

How to export Java app as runnable?

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.

Create compile script for Java

I have downloaded an Eclipse project and I want to be able to have other people compile it without using Eclipse. It is a fairly large Java project that is still being worked on. How would I make a compile script that compiles like eclipse?
I highly recommend you look into Maven. Basically you'll define a Maven pom file in the root of your Eclipse project directory which will contain your dependencies (jars) as well as compile and assembly configuration. With this in place you can simply checkout a project and run a maven build command against the local directory you checked out to and an executable/deployable package will be created.

Java project with only build.xml [duplicate]

What is the file build.xml?
I was wondering if it is a possibility to import this project in Eclipse or Netbeans using this build.xml. I tried to import the project but I get some errors since one part is created using J2ME and the other J2SE and I guess this file should be the configuration.
build.xml usually is an ant build script.
It contains information necessary to build the project to produce the desired output, be it Javadocs, a compiled project, or a JAR file.
I believe Eclipse has ant built-in, so it should be possible to execute the build.xml by choosing "Run As..." and "Ant Build".
The build.xml file, if it is an ant script, is not used to import the project into an IDE like Eclipse or Netbeans. A build script is used to build the project (or produce some desired output) rather than an mechanism for importing the project into an IDE.
As mentioned by #coobird this is an ant build file. Although IDEs such as Eclipse and Netbeans have ant support built-in, it is also possible to run ant from the command-line and this may be the simplest way to get started if the project has been well created.
See http://ant.apache.org/
for docs.
If you want to try this approach, install ant, cd to the directory with build.xml and issue
ant
Eclipse can be told to build using an Ant script, but you can also use Ant itself.
build.xml file is an Ant(Apache) script.
you can find more information on Ant & build.xml here
In java project build.xml file is used write the ant script.
And from that ant script you can generate war file and can deploy on Tomcat server

How does tomcat make use of build.properties and build.xml?

I'm following a tutorial to set up a skeleton application for tomcat :
http://maestric.com/doc/java/spring/setup#build_files
But I don't understand how build.properties and build.xml actually works.
I'm using windows XP and copied the following even though the required directory doesn't exist:
appserver.home=/usr/share/tomcat5.5
appserver.lib=${appserver.home}/common/lib
Really confused now:(
Tomcat doesn't use the build.xml and the build.properties files, these are for Ant which is a tool to automate the build of the application. The script shown in this tutorial is pretty basic, it defines 2 targets to compile sources and to clean compiled classes. And you would use them like this:
ant build
or
ant clean
The appserver.lib property is used to build the class path required to compile sources. It is derived from the root of your Tomcat installation directory and is used to find the JAR for the Servlet API that you need to compile sources. If you decide to use this Ant script, you should update the appserver.home property to match your install. For example:
appserver.home=c:/apps/tomcat5.5
appserver.lib=${appserver.home}/common/lib
But to be honest, the whole setup is a bit messy (it's straightforward, but messy):
you shouldn't bundle the servlet-api.jar in WEB-INF/lib as suggested
I don't like to develop directly under Tomcat's webapp directory (but this is maybe subjective).

Categories

Resources