Netbeans doesn't see classes from referenced library - java

I'm using netbeans 7.2 with NBAndroid extension. In my android project, I'm referencing a library (ActionBarSherlock) that is not in jar (can't be, for some reason). The problem is, that netbeans doesn't see classes from that library and gives me errors (package does not exist etc.) However it builds and runs OK, the library is added correctly. Netbeans just doesn't see it.
Here is a screenshot.
Here is similar question, no solution package com.actionbarsherlock.app does not exist
Is there a way to fix this? Thanks for help!
EDIT: So I found a way to solve this, it's more a workaround than a solution. I created a jar file from the library classes called classes.jar. I put it in the libs folder, so netbeans sees it. Than I created custom_rules.xml (it's imported via build.xml). In it I move classes.jar away from the libs folder, so I can build it, and in the end I move it back.
<?xml version="1.0" encoding="UTF-8"?>
<project name="imported">
<copy file="libs/classes.jar" todir="./" />
<delete file="libs/classes.jar" />
<target name="-post-compile">
<copy file="./classes.jar" todir="libs/" />
<delete file="./classes.jar" />
</target>
</project>

The errors you see are because NetBeans doesn't recognize the packages, classes, methods, etc. When you build or run the program, NetBeans resorts to the Android project's Ant script, which apparently is configured correctly to find the classes. As far as compiling and running, you won't have a problem. However, if you want to use NetBeans' autocomplete and error-detection features, you need to configure it to detect your libs. To do this, just right click on your project name in the Project pane and click Properties from the context menu. Next, click on Libraries under Categories on the left. Then click Add JAR/Folder and navigate to the folder with your third-party library. You can select one of the options for the path then click OK. Now NetBeans should be able to find the identifiers and help you write your code.

Rather than doing some tricks in your build script I'd recommend you to use ActionBarSherlock as a library project. You should be able to do this in project customizer (select project, right click, choose properties).
Also see 'Including in YOur Project' bullet 2. in http://actionbarsherlock.com/usage.html
-Radim

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.

How do I add a directory to the eclipse classpath?

I'm attempting to run an already existing eclipse project created by another person.
After importing it to eclipse, and attempting to Run As->Java Application, it fails because it cannot find a .properties file in bin/resources
I printed out the classpath eclipse was using
logger.info(System.getProperty("java.class.path"));
and sure enough, it includes bin, and all the lib/*.jars, but not bin/resources. Copying the .properties file into bin makes the program work, but I wanted to understand how to add a directory to the eclipse classpath.
I tried several things, none of which worked
export CLASSPATH=$CLASSPATH:/home/me/programdir/bin/resources
This didn't work. I understand it's not a desirable way to approach the issue, but I had thought it would fix the problem (I have more of a windows than linux background so perhaps I am missing some nuances of system variables in linux)
Next up, I tried modifying the VM arguments in the Run->Configurations dialog in Eclipse
-classpath "/home/me/programdir/bin/resources"
No luck here either, which confused me, as I was sure it would work and seemed like a reasonable solution to a specific program needing one additional folder added to the classpath.
Next I tried modifying build.xml directly. I found the part that defines the classpath and added my own line for bin/resources, as follows:
<path id="classpath">
<fileset dir="./bin/resources" includes="**/*.properties"/>
<fileset dir="./lib" includes="**/*.jar" />
</path>
this too was unsuccessful. This perplexed me even more, so I commented out the entire path element, and the classpath printed out by the logger was unchanged, so it is apparent that whatever classpath eclipse was using, it certainly wasn't this one. This seemed to me the best solution, had it worked: the build.xml file could be checked in with the correct additions to prevent future users from experiencing the problem.
Next I tried the IDE approach. Run->Configurations->Classpath-> User Entries->Advanced and simply added the bin/resources folder. That worked perfectly, the program finds the properties file, all is well. However, I am dissatisfied that my previous efforts failed without me really understanding why. It seems that each one should have worked.
Additionally, I want to ensure that I fix this problem in such a way that it is captured by the code I check in so that subsequent users do not have to go through the same steps. My solution is thus not very satisfactory as I am not sure what actual piece of code changed, and thus cannot verify that the 'fix' is checked in.
How do you find the actual definition that eclipse is using for its classpath? I had thought it would be the build.xml classpath definition, but that did not seem to be the case at all.
In Eclipse, there is a build classpath and a runtime classpath. There is also the build output location, which by default is bin. You don't want to add resources directly to bin because Eclipse can delete its contents when doing a clean build. What you need to do is add a resources folder in your project to contain any non-Java files that you want included in your build output.
To include the contents of this resources folder in the build output (bin), right-click the project and select Properties. In the Project Properties, select the Java Build Path section, then the Source tab.
Use the Add Folder... button to select the resources folder from your project, then OK to save the changes. At that point, Eclipse will automatically copy everything from resources into bin when it builds.
This is for a maven project:
Right click on project
click on run configurations
click on the classpath tab (Oxygen Eclipse)
click on user entries
click on Advanced
first radio selection default should be 'Add Folders'
click OK
Follow these steps to get this issue fixed:
Right click on Project
Click on Run As and select Run Configurations
Click on the classpath tab (Oxygen Eclipse)
Click on user entries
Click on Add External JARs.. and choose the downloaded JAR file
Click Apply and run your project...
Right Click on the project-name in Package Explorer, select Properties, select Java Build Path on the left, select Source tab on the right, click on Add Folder, browse through the project's directories to select the resources folder or whatever you need to add to the eclipse classpath, hit OK, again hit OK. Done.
If you don't want the properties file to be copied to the bin folder, you can try the following:
Right click your project, select Build Path, select Configure Build Path..
Select Libraries tab
Select Add class folder..
Add your resource folder.

Change destination folder of JAR files in Netbeans 7

I'm working on a multiproject solution for a client and we're trying to have all our builds automatically go into a predetermined BIN folder. All the C++ projects were easily enough moved over but the Java side has proven not so easily configured. When I go to the project's properties and go to Build->Packaging the "JAR File:" text box shows a read-only text-box pointing to "dist/App.jar" (which I would like to change to "../../../bin/App.jar". Any thoughts on how to do this?
You can change the dist-folder by editing the dist.dir key in the project.propertiesfile which is located in the nbproject directory from
dist.dir=dist
to
dist.dir=../../../bin
In my case the key is in line 24. The name of the jar-file itself is determined by the dist.jar key, if you would need to rename the file aswell.
add into build.xml
<target name="-post-jar">
<copy file="${dist.jar}" todir="drive\path\"/>
</target>
Did you check under the menu
[Run] --> [Set project configuration] --> [Customize]
You have plenty of options to change there, it is easy than that.
Good luck

Setting Different Java Class Paths in Eclipse

I have been given an application which uses a build.xml file for building purposes. I have very little knowledge of Apache Ant and the classpaths seems to be the following:
<!-- Classpath -->
<path id="development-classpath">
<fileset dir="${libs.dir}">
<include name="**/*.jar"/>
</fileset>
<pathelement location="."/>
<pathelement location="${classes.dir}"/>
<pathelement location="${configuration.dir}/langs/"/>
<pathelement location="${fits.dir}/xml/nlnz"/>
</path>
As I want to use Eclipse own building facilities I would like to assign these classpaths variable in Eclipse, which I don't have much experience with. How do I do it?
Go to your external tools configuration, select your ant build, and click on the Properties tab. You will probably have to un-click "Use global properties...". Then it is simply a matter of adding properties. This will allow you access to the variables built into Eclipse such as ${project-loc} and these properties will be available to ant as if you had set them in the ant file itself.
Here's the manual way to configure Eclipse to mimic what ant will do using the build.xml file. This should get you started:
Open Eclipse, open the "Java" Perspective. Right click the project and choose "Build Path / Configure Build Path". Alternatively click "Project" Menu, and choose "properties", then click "Java Build Path". Click the Libraries Tab. This is where you tell Eclipse where the jars needed build your project are located.
From the build.xml snippet, the first "<fileset>" specifies that all the jars you need are under the <project>/libs directory (I'm guessing that ${libs.dir} corresponds to "libs" but you might want to double check. It should be defined near the top of build.xml). So click, "Add Jars...", navigate into <project>/libs, highlight all the jars inside the directory and then click "Ok".
The first and second <pathelement> tags are telling ant to use the current directory and the classes directory. Eclipse should already take care of this by default, but to double check, click on the "Source" Tab (should be in the same "Java Build Path" dialog as the "Libraries" tab). The default output folder shows where eclipse will compile java code to .class files. The Source folders on build path shows where all your source code is that Eclipse should try to compile.
Finally, The last two <pathelement> tags tell ant to use some resource/config files under ${configuration.dir}/langs and ${fits.dir}/xml/nlnz. You can add these similar to the way you added the jars. Click "Libraries" Tab. Then choose "Add Class Folder" and highlight both the "langs" and "nlnz" folders.
Hope this give you some insight into where Eclipse looks for jar dependencies, class files and resources.
Having said all this, as you become more familiar with Eclipse and build tools, you'll probably discover that this probably isn't the best way to go because it's very easy for the build.xml and eclipse configuration to get out of sync. There are ways to tell eclipse to build using an ant build.xml file (check out File->New Project->Java Project form Existing Ant Buildfile). And there are also ways to run ant targets from within Eclipse (check out Window -> show view -> ant).
Probably you need to put build.properties files at same path which build.xml is.
This build.properties will looks like similar to this:
libs.dir=path_where_libs_are
classes.dir=path_where_classes_are
configuration.dir=etc
fits.dir=etc
This will allow to run your ant script with these configuration values inside and outside Eclipse.

Run NetBeans project just with ant

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>

Categories

Resources