Run NetBeans project just with ant - java

I have a sample code that was built with Netbeans.
It has a build.xml file so I downloaded ant and try to run it.
I've got this error message:
...... nbproject\build-impl.xml:76: Platform is not correctly set up
For what I can see, this is fixed by "simply" downloading Netbeans and running the sample from there, but... I don't want to install it to run a 10 files sample.
Is there a workaround to run Netbeans projects with Java? What's the correct .properties file I have to modify?

It is possible to run the NetBeans generated projects straight from Java/ANT, but you may need to manually set some of the properties and/or add paths to jar files.
Unfortunately, NetBeans tends to include taskdef's using their own JAR files and reference properties that are defined only in the /nbproject/private/private.properties files, which usually get set when you first open the NetBeans project or modified as you edit the project in the IDE.
If you inspect the build-impl.xml you should be able to find the property and derive what value needs to be set(OS platform), then either:
create/set the property in the
/nbproject/private.properties
add that property definition in the
parent build.xml
pass in the commandline when invoking your ant
target using -DPlatform=Foo
Personally, I like the structure of the NetBeans generated ANT files and targets, but hate how much custom/proprietary stuff they jam in that makes it hard to run without NetBeans.
For example:
ant -Dplatforms.JDK_1.7.home=/opt/jdk

I've just successfully built NetBeans project with ant. These were the things I had to do:
Copy <NetBeans-folder>/java2/ant to a "Netbeanless" machine
Copy nbproject/project.properties to, say, ant.properties
Replace every ${} expression in ant.properties with it's value
Add platform.<platform-name>.home=<path to platform>
Add libs.CopyLibs.classpath=<path to nb-ant>/extra/org-netbeans-modules-java-j2seproject-copylibtask.jar
Add other needed classpaths into javac.classpath (e.g. path to servlet-api.jar)
ant -propertyfile ant.properties
It works, but doesn't make me happy. I would either like to find the way to reuse project.properties, or to automatically translate it to a "resolved" version (step 3). Build could then be automated.

I just went through this exercise with my NetBeans 7.0 project. What I was able to do was copy my build.properties file from my .netbeans\7.0 directory on my Windows system and make a server.properties file for my build server. This isn't much of a stretch, since every developer's build.properties may vary, so having another file for the server is to be expected. I then put together a simple server-build.xml file that references this file and does only the basics of init, compile, dist, and clean. I committed these two files to my CVS repository at the top level of my project directory, since they don't conflict with other project files and serve as a reminder in case something needs to be updated. Now I can build my project on my CI server with "ant -f server-build.xml" and everything just works.
My whole init section looks like this, giving my server paths priority, but including the necessary information from the NetBeans project properties.
<target name="init">
<property file="server.properties"/>
<property file="nbproject/project.properties"/>
</target>
I also had to do something similar when defining the ant tasks for my nested projects:
<target name="compile">
<ant antfile="${project.MyProj-common}/build.xml" inheritall="false" target="jar">
<property location="${build.dir}" name="dist.ear.dir"/>
<property file="server.properties"/>
<property file="${project.MyProj-common}/nbproject/project.properties"/>
</ant>
...
</target>
I had to copy the j2ee.platform.classpath from project.properties to my server.properties file to ensure the references to j2ee.server.home resolved as I needed. I didn't expect to have to do this, but the classpath was wrong otherwise, causing the build to fail.
Thanks for the information on this question, as it helped guide me to this solution.

I just faced the same problem. I hope I could get rid of netbeans to go to eclipse and maven, just for that.
But here is a good link to export a ant built project from netbeans into a continuous integration server (or could be into any other IDE too).
Technique is going pretty well. Here is a summary:
install netbeans on the CI server (there is an option to do it without gui, use -silent on the netbeans installer)
include nbproject in SVN
add to ignore list the private folder
create your own private folder on CI server
make it point to a folder of your CI server (mimicking a private user folder in the account used for CI)
copy a real folder from a user to this folder and change every path (replace strings) to point to your netbeans install on your CI server.
And it should work.

Just to add to Mads' answer... usually, you need to install and open Netbeans at least once on the target machine. The ANT projects also rely on a few settings from the USERDIR/.netbeans/... directory. This may have changed with 6.5+.
This will get some of the base settings configured and define the classpath's to netbeans jars. If your dependencies (i.e. libraries) or project is being run from a different directory since the last time you opened the project in Netbeans, you will need to tweak a few settings in the private.properties file as Mads' described.

I'm using Netbeans 6.8 and java projects that were created with Netbeans can be run from the Netbeans auto generated build files with just ant on the cli. All the regular targets like ant compile,ant run,ant clean, etc "just work". (I'm using Fedora 13 if that matters)

You can use this repo just for that https://github.com/albfan/ant-netbeans
It overcomes all the oddities of netbeans wrapped ant config so you just need:
To compile:
$ ant.sh compile
To run:
$ ant.sh run
To whatever (autocompletion):
$ ant.sh <tab><tab>

Related

How to use proguard on intellij IDEA?

I am using Intellij IDEA to develop java desktop application. And I want to obfuscate my source code using proguard. How to integrate/use proguard on Intellij IDEA 2016.1.14?
1. add plugin Intellijguar2
2. in Project Structure ->modules->obfuscation press download yguard as it prompts. It brings to a html page where yguard is placed next to right edge of the page. Unzip.Get jar. Navigate to the jar. Set its path. Uncheck pedantic error-checking (optionally) , define project's MainClass. Ok it.
3. Build -> Build project -> build artifacts and get ordinary executable jar.
(If you cannot see artifacts enabled in Run menu go to file-> project structure ->artfact and create by + new item with dependensy to the main class)
4. build -> obfuscate *** module
5. Add YourProject/out/production/YourProject/firstfolder_of_packagename containing your project's *.class files(mine was 'uz', e.g.) .
6. Remove Module compile output Assign a path to the jar to be obfuscated below and press 'build'
7. Open obfuscated jar with zip program. Make sure the class files are all obfuscated by JD-GUI app.
8. cut off META-INF folder and add META-INF one from executable inobfuscated jar and also folders like libs or assets(or find out them in artifacts you setup previously) manually.
In turn you'll get an obfuscated executable jar
E.g. for json lib finally I've got the result view in zip editor:
I have not tried this (i like to find the easy way first) but, it should work if you follow the steps. I will try this and report back.
Go to this link and learn how to create an Ant build file for IntelliJ IDEA
Go here to download yGuard
Unpack the yGuard archive and navigate to the doc directory.
There is an html "how to" file. Read up on that, and you should be ready to release minified code.
Optional: search the net for yGuard tips & tricks to get the most out of your builds.
--- WORKING-NOTES ---
[1.0] In IntelliJ 2017, there is an option to generate the Ant Build File on the build menu. Build->Generate Ant Build File The settings that work for me are single-build-file, with everything else checked, using the supplied project name.
[1.1] View->Tool Windows->Ant Build this should get you where you need to be with the knowledge you got from the 5th step of the link at step 1 and step 4 of this answer.
Here is my working yGuard task:
<target depends="artifact.project" name="yguard">
<taskdef name="yguard"
classname="com.yworks.yguard.YGuardTask"
classpath="yguard.jar"/>
<yguard>
<inoutpair in="${temp.jar.path.project.jar}"
out="${artifact.output.project}/project-release.jar"/>
<shrink
logfile="shrinklog.xml">
<keep>
<class classes="protected"
methods="protected"
fields="protected"/>
</keep>
</shrink>
</yguard>
Then you modify your "all" to look like this:
<target name="all" depends="build.modules, build.all.artifacts, yguard"
description="build all">
<!-- Delete temporary files -->
<delete dir="${artifacts.temp.dir}"/>
</target>
And you have to REMOVE the delete temporary files action from the build.all.artifacts target, so when you get to the all target, the files are still available.
Just like it says in the yGuard html doc at the bottom, IntelliJ will complain about your yGuard syntax, but yGuard will still work.
If anyone can clean this up, feel free. It works for me, your mileage may vary. Also, you WILL be able to create a really tiny ant build xml, and use the Project Structure->Artifacts->Post Processing, after you have added your "tiny" ant build as noted in WORKING-NOTES: [1.1] "5th step". Some hand-hacking will definitely be required for this.

Eclipse won't recognize the src files from a git import

I imported a huge repository from gitlab for my lab project. It is structured like this:
project_folder [master]
JRE System Library
main_project_stuff
anthome_build_stuff
com
config
doc
...etc
Most java projects I have handled are structured like this:
project_folder
src
bin
Because there is no source folder, java will not recognize any of these folders for what they are. As a result, it can't build them and give me error messages. This only works when I drag them into a src directory that I make with eclipse, which is not ok with the lab because I can't be reorganizing files on the master branch when I eventually commit to it with git.
So how can I make eclipse recognize these as src .java files that are in packages like package: com.robot.robotComponent.java? Without actually moving them into the src directory manually?
TL;DR How do I make eclipse stop thinking of these as folders and realize they are actually packages that are stored as a folder tree?
Note: This project is not configured to be used only in eclipse, but is a general project. So I have to make my own .project and .classpath files to make it readable to eclipse.
(I am using Ubuntu 14.04 but maybe that is not relevant. I got it to work by brute force making the project_folder the actual src folder, but then there are a bunch of compile errors because the project really isn't meant to necessarily be built in eclipse? It builds from the command line using run-registry command but I want it to at least show me useful errors like when I make newbie mistakes like one too many "}" or forget to implement all functions in an interface, eclipse can fix that automatically for me with a red error message. Now however I am getting many strange errors, like it can't find project_folder.SuperComponent.java which I know is there!)
Update: I guess this needs to run a registry from ant? And then build it from an ant build.xml. I'm not sure how to get the command line arguments to work with eclipse.
If you use the Eclipse Smart Import feature, the project will be detected as a Java project (even without the .project file) and automatically configured (.classpath, etc.). Instead of using File > Import... do the following:
Clone the Git repository
In the Git Repositories view right-click the Working Tree node and choose Import Projects...
Note, the more up-to-date your Eclipse IDE is, the better the automatic detection and configuration will work.

How to solve "invalid resource directory name", resource "crunch"

lately I am getting this error from eclipse:
"invalid resource directory name", resource "crunch", type "Android AAPT Problem"
what is this problem and how to solve it?
Fix:
from the menu click Project->Clean...
a popup window will appear. select the check box for the project that is making this issue then click OK.
wait and see, that is it.
Ant and the ADT Plugin for Eclipse are packing the .apk file in a different build chain and temp generation folders. Crunch is created by the ADT. Best to do is to start every step with a clean if you switch between the tools. use ant clean if you used the ADT from eclipse before. Use Projects -> clean ... in Eclipse if you used ant before. Hope this solves your problem. Also restarting Eclipse could help.
I had the same issue: invalid resource directory name: D:\work\merge\Client_2_24\Client\bin\res/crunch. I tried Project->Clean but didn't work. Then I directly deleted the directory crunch and it worked :)
This problem is resulting of and Android requirement that in res folder only can have drawable, drawable-xhdpi, and so on. crunch is not one of this.
It's commented here.
https://stackoverflow.com/a/7168818/2411379
First of all, I find that this annoyance manifests itself when alternating between building my code in Eclipse and building it via ant on the command line.
The solutions that involve cleaning the project(s) are fine, however, my source tree takes more than 5 minutes to build given the fact that much of the code makes heavy use of templates. I try to avoid cleaning whenever possible to keep from having to do a complete rebuild. So here's my solution:
Add the following to build.xml:
<target name="-pre-build">
<delete>
<fileset dir="${basedir}" includes="**/crunch/**"/>
</delete>
</target>
This delete task in ant will recursively remove all crunch directories beneath the directory where the build.xml file is located. This task will run whenever I do an ant debug/release on the command line, which means I'll never have to clean my project as a result of this crunch nonsense again!
I could solve this issue by doing "ant clean debug". The above suggestions of cleaning the project from Eclipse didn't work for me.
If you don't want to use eclipse and get this error message with ant release -Dsdk.dir=$SDK_ROOT, you can just run ant clean -Dsdk.dir=$SDK_ROOT
This happened to me when I incorrectly imported a project to eclipse using "New Project ->Android Project from Existing Code".
It caused the crunch directory to turn up somewhere unexpected.
Importing using "Import -> Exising Projects into Workspace" solved it.
create a file called ant.properties in the folder where your manifest file is.
add aapt.ignore.assets=crunch:(whatever the default values are in your android sdk/tools/ant/build.xml file commented above the property )
when using cordova from the command line it uses ant
when using eclipse it uses android package manager
package manager compresses the png files and puts them into crunch, crunch is an invalid folder name for ant, if eclipse is set to build automatically, everytime u delete crunch it will be built again by the android package manager. you can test this by right click project, propteries, builders, unchecking android package manager, deleting crunch folder, and it will never come back again, you will also never have compressed png files again.
deleting the folder without doing any of these things and with eclipse open is just rolling the dice, will eclipse auto generate the crunch before cordova gets to the package-resource stage
when I updated android SDK build tools to version 18 and 19, I had the same issue.
My solution is:
go to google service library project - properties - builders - uncheck all builders
delete all the files under bin folder
run ant task to clean this project.
I believe the problem was that eclipse would build automatically, and generate files that are conflicted with ant build.
First of all try to Clean your project. If it works then well and good otherwise move into
the bin -> res -> there is one folder named Crunch. Just delete that folder directly and clean the project again.
It will solve the problem.
while my error is
Error:Execution failed for task ':app:mergeDebugResources'.
/path/to/project/app/src/main/res/drawable-xxdpi: Error: Invalid resource directory name
notice "drawable-xxdpi" is not valid folder name, while "drawable-xxhdpi" is right.
Some of yours folders' names might not be allowed by the Android standard: check the folders you made, and rename them.

How to Build "Thinking in Java" Example Files to Run in Eclipse IDE?

I wanted to learn Java, as recommended by most of the people I downloaded "Thinking in Java" eBook and Source Code Example Lesson files from mindviewinc.com it's download location is this http://www.mindviewinc.com/TIJ4/CodeInstructions.html
These files don't work with Eclipse IDE for some reasons. So we have to build the files using the Build.xml file included in it's source code (Honestly, I do not have any idea what does "build" means here)
I tried building myself but of no use.. It did started building but after reaching some folder it stopped and gave error to install jboss-osgi-installer-1.0.0.jar .. After downloading it, I had no idea what to do with it, so I double clicked it and it asked me the location to install and I installed it in the default location, which was C:\Users\Username/jboss-osgi-1.0.0 and when I tried C:\Program Files\Java** the installation gave error that "this directory cannot be written, please choose another directory!" but anyway I installed it in the default location and it installed..
and again when I tried to build it, it gave me the same error and asked to Install Jboss....... I'm stuck.. This is happening again and again, wasted many days and didn't even crossed 100 pages of it's pdf because of this reason. It's a good eBook though.
I'm stuck in this from months... Please some one build it and make it like a project which could be easily and send me the project file, it would be really appreciated as I can not the solution to this anywhere on search engines..
Ok here's what I did to succesfully import the sources found in the "accesss" folder of you code samples:
Download the javassist jar as per Bruce's instructions, from here:
http://repo2.maven.org/maven2/javassist/javassist/3.9.0.GA/javassist-3.9.0.GA.jar
You must create an Eclipse project for the sources that are used as dependencies for most of the examples, that's the sources in the "net" folder:
in eclipse make new java project.
select the "net" directory, copy it, then in eclipse select the "src" entry in the project you just made, right click on it and select paste.
after eclipse finishes and auto-builds, it will complain about the missing javaassist jar. Right click on the project ->properties->java build path (on the left)->libraries (on the top)-> add external jars... (on the right), browse and select the javassist jar from where you down load it.
now the "net" project should be ok in eclipse
Now let's create the project for the sources in the "access" folder:
create a new java project in eclipse
Now, because Bruce structured his classes like crap, we'll have to make the proper structure in eclipse for them. This means:
a.create a new package called "access"
go to where your the access folder is on your drive, open it, select ONLY the folders (cookie2, desser, mypackage), copy them, then return to eclipse, right click on the "access" package u just made and click paste
now go back to the access dir on your drive, select ONLY the .java files (all of them, not the dirs), copy them, the back to eclipse, click on the src entry in the access project and click paste.
The access project in elcipse will still complain about missing classes form the "net" resouce. Right click on the project in eclipse->properties->java build path->projects (on the top)-> add(on the right)-> check the net project you created earlier-> click ok twice.
Now it should be ok
I hope you got the idea on how dependencies work in eclipse with this example.
For the other parts of you code samples, create separate java projects, add the classes to the proper package (create the packages if Bruce forgot to), and if you are missing external jars, go to http://search.maven.org search and download the jars. Good luck
"This code is designed to work outside of IDEs. Because packages are not introduced until later chapters, and some of the fancier IDEs like Eclipse require all code to be in packages, if you want to use the code inside those IDEs you will have to make some adjustments" - yep, bruce eckel would say that, cause that's the kind of guy he is. He also said there's ONLY checked exceptions in java, and asked for unchecked exeptions to be added as a bonus.
Now, if your sample code has no package, aka it's in the default package, aka when you look in the .java file, right at the top there's no line saying "package what.ever.bla" then all you have to do is:
-in Eclipse create a new Java se project
- open a windows explorer, go to your .java files, select them and copy them
- then in eclipse, right click the src folder in your project and chose "paste"
The files should be added to a "default" package in your eclipse project, without any error
Here is the simplest steps that I find to create a clean Eclipse project for the TIJ4 source code:
Download the source code zip from: http://www.mindviewinc.com/TIJ4/CodeInstructions.html, and unzip it to a folder, say 'TIJ4-code';
Download the additional libraries as in the previous URL, copy tools.jar from your current JDK's lib directory, and put them into TIJ4-code/lib;
Go into the 'TIJ4-code' folder, and run 'python Eclipse.py' (you need python installed on your system). This script adds the missing "package xxx" line to those source java files which don't yet have them, so as to make Eclipse happy. It also generates the Eclipse project files, which doesn't work well for some reason (beyond my limited knowledge on Eclipse), but they aren't needed here;
Open Eclipse and create a new empty java project, say with name 'TIJ4', choose JRE version 1.5, and "create separate folders for sources and class files";
Copy TIJ4-code/lib directory to your new Eclipse project folder;
Right click on the new project and select "Build Path" >> "Configure Build Path", then go to tab "Libraries", "add jars", then select all the jars under your project's lib folder;
Go to "File >> import", choose "General >> File System", select the unzipped folder 'TIJ4-code' as your "From directory". Check the root 'TIJ4-code' in the left panel of the import window, Click on "filter types" and select the .java, so that unnecessary files of other extensions, like *.class, *.py, *.xml, etc can be ignored during the import;
The zipped folder is no longer needed after the new Eclipse project is built. I hope you find this instruction helpful, and have fun learning TIJ4.
If you want to use this code in IDE rather than Eclipse and so on, for example in Intelij Idea or any other, the best way is to compile necessary classes from the TIJ4 with Ant, but it is not easy for the beginners, although it will be a good practice for you. You should make some changes in build.xml files which exists in every directory of TIJ4. Why?
to change version of Java(now we all use Java version higher than 1.5)
to include javaassist.jar library in the "classpath"
to put all compiled files in 'bin' directory(by default without adjustments TIJ4 build.xml puts .class files in the same directory with .java files. That is not a good deal.
For example you need to import and use within IDE(I use Intelij Idea) the class named net.mindview.util.Print ( see Chapter 3):
download TIJ4 and unzip in any directory you want(for example it would be c:\eckel)
download and install AntApache (you can find detailed instructions how to do it in Google) The result is - type in your console ant -version and you should see the answer. So Ant is working.
download javaassist.jar from http://jboss-javassist.github.io/javassist/ . Unzip to any directory, take out javassist.jar and past it to c:\eckel\net
4.There is build.xml file in c:\eckel\net. Now you should make adjustments. Open it with NotePad++ or other editor.
If you don't have Java 1.5 installed on your PC, so change all 'vesion1.5' to 'vesrion1.8' or other version that you have installed.
after tag description put property name = "classpath" value="./;./javassist.jar"/>
<property name = "src_util" value="./mindview/util"/>
<property name = "src_simple" value="./mindview/simple"/>
<property name = "src_atunit" value="./mindview/atunit"/>
<property name = "destdir" value="bin"/>
Then we make targets that will complile util simple or atunit automatically
Change tag available <available
classname="javassist.bytecode.ClassFile"
classpath="${classpath}"
property="javassist"
/>
Now classpath of available = property named 'classpath'. Classpath is the path where java compiler looks for .class files because some of your .java files want them by having import statement. There won't be error message "You must install the Javassist library " any more
Create targets for compiling util simple and so on See the following link with code
Code with targets
You can modify exists targets not to write all the code.
at the top of build.xml file change default="simple"
open cmd, then cd c:\eckel\net , then ant -simple or just ant (simple will run as default)
Please write if you will have some problems. Possibly i forgot mention about something.
Go to Intellij Idea, press Ctrl+Alt+Shift+S
Press +,
Library or directories
choose your c:\eckel\net\bin
5.now you can use import statement for all classes that exist in net.mindview
6.you can make the same with other dirs.
If you wish to you skip the entire Ant build process, I have converted the project to a Maven project (over JDK8), which you can simply import into your Eclipse workspace.
https://github.com/gauravojha/tij4-maven
Thanks to #Sergey Oliv's example. In addition, I needed to provide uncompiled javaassist package with all java files in the 'net' folder too. The .jar wouldn't work by itself.
I must say, this was a lot of work to get going.

Where to put external archives to configure running in Eclipse?

As a Java/Eclipse noob coming from a Visual Studio .NET background, I'm trying to understand how to set up my run/debug environment in the Eclipse IDE so I can start stepping through code.
I have built my application with separate src and bin hierarchies for each library project, and each project has its own jar. For example, in a Windows environment, I have something like:
c:\myapp\myapp_main\src\com\mycorp\myapp\main
...and a parallel "bin" tree like this:
c:\myapp\myapp_main\bin\com\mycorp\myapp\main
Other supporting projects are, similarly:
c:\myapp\myapp_util\src\com\mycorp\myapp\util
(and a parallel "bin" tree), etc.
So, I end up with, e.g., myapp_util.jar in the ...\myapp_util\bin... path and then add that as an external archive to my myapp_main project.
I also use utilities like gluegen-rt.jar, which I add ad external dependencies to the projects requiring them.
I have been able to run outside of the Eclipse environment, by copying all my project jars, gluegen-rt DLL, etc., into a "lib" subfolder of some directory and executing something like:
java -Djava.library.path=lib
-DfullScreen=false -cp lib/gluegen-rt.jar;lib/myapp_main.jar;lib/myapp_util.jar;
com.mycorp.myapp.Main
In the IDE, however, When I first pressed F11 to debug I got a message about something like /com/sun/../glugen... not being found by the class loader.
So, to debug, or even just run, in Ecplipse, I tried setting up my VM arguments in the Galileo Debug -> (Run/Debug) Configurations to be the command line above, beginning at "-Djava.libary.path...". I've put a lib subdirectory - just like the above with all jars and the native gluegen DLL - in various places, such as beneath the folder that my main jar is built in and as a subfolder of my Ecplipse starting workspace folder, but now the debugger can't find the main class:
java.lang.NoClassDefFoundError:
com.mycorp.myapp.Main
Although the Classpath says that it is using the "default classpath", whatever that is. Bottom line, how do I assemble the constituent files of a multi-project application so that I can run or debug in Eclipse?
if you want project A to have project B (and its libs) on its classpath, then goto project A's properties, select Java Build Path, then select the Projects tab. Here you Add project B.
Although you may be generating jars for each project, eclipse doesn't pay much attention to the jars. If you are going to have lots of java projects with lots of 3rd party dependencies, you may want to look at something like m2eclipse.
The recommended way of doing things is to configure the build path. This is a nice article describing the process.

Categories

Resources