Setting Different Java Class Paths in Eclipse - java

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.

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.

Netbeans doesn't see classes from referenced library

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

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.

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