I have a third-party library in my SVN repository and I'd like to associate source/javadoc with it locally in Eclipse. I.e., there should be some local setting (for example, an entry in the local.properties file) that associates the source/javadoc with the JAR file, but which doesn't introduce local dependencies into the repository via .classpath. Ideally I'd have
lib_src_dir = /my/path/to/lib/src
in local.properties and then
<classpathentry kind="lib" path="lib.jar" sourcepath="${lib_src_dir}">
in .classpath. Can this be done?
[EDIT] #VonC's answer is helpful... Is there a way to load Path Variables from a text file (e.g., local.properties) instead of going through Window -> Preferences -> General -> Workspace -> Linked Resources?
I believe this would be better achieved through:
the creation of a linked folder combined with
the declaration of a linked resource
The linked resource defines a path variable which would be equals to /my/path/to/lib/src
The linked folder would refers to your linked resource
(you can use a variable and not a fixed path, with the "Variable" button)
The variable is actually always local (to one's workspace), and will be modified through the Linked Resources preference screen.
The linked folder can also be... a linked file, thus allowing the reference of an archive through a relative path (relative to the variable).
Then this linked file (here a linked archive) can be associated to your classpathentry in the "source" attribute.
The problem with Linked Resources is they are local to the workspace, in the preferences.
You can export the preferences in a [myPrefs.epf] file, and then trim the exported file in order to leave only the lines containing pathvariable:
/instance/org.eclipse.core.resources/pathvariable.MY_DIRECTORY=/my/path/to/lib/src
Anyone can then import this special preference file, which will only affect the "Linked Resources" part.
That solution is not very satisfying, since the .epf preference file can not be loaded automatically in the project.
When I setup a project with a linked resources defining a path, I always leave a big README.txt at the root of my project, in order to incite the user of said project to define that same linked resources with his/her own fixed local path.
Several bugs are in progress to enhance this situation or around the Linked Resources topic.
Especially:
Exporting a project with linked resources
Relative paths without variables
Have linked resources relative to workspace paths
Would like to use path relative to workspace root
DevByStarlight mentions in the comments the project (not very active since Oct. 2011) workspacemechanic.
The Workspace Mechanic automates maintenance of your Eclipse environment by tweaking preferences, adding extension locations, and so on. You can use it to:
Create a consistent environment among groups as large as the entire company, your local team, or even among your own many workspaces
Save time setting up new workspaces
Create tasks that ensure your favorite new preferences are applied to all your current and future workspaces. (This is one of our favorite features!)
The key to the Workspace Mechanic's behavior is the Task.
A task describes a simple test and an action that, when run, changes the environment so the test will subsequently pass.
Tasks can come in many forms: preference files, Java classes, Groovy scripts and Eclipse extensions. You can easily define your own Tasks.
It comes with a collection of scripts:
workspace-mechanic
workspacemechanic-settings
I just figured out a simple answer to this (in Indigo) after working on it in the background and free moments for a couple of days. The easiest way I've found is to expand your project in the Project Explorer, go into your Referenced Libraries, right-click the appropriate referenced JAR and click Properties. In there you have the options to designate a JavaDocs location. Enter the location of the folder that contains index.html and packages-list, files that are part of the Javadocs. Piece of cake!
The only problem I see so far is that I bet you need to do this in every projects that references that library.
You can do this with classpath variables.
Each developer creates a couple of new variables at Window -> Preferences -> Java -> Build Path -> Classpath Variables.
Define a variable (say, JAVA_LIB_DIR) that points to a directory containing the third-party JAR (or JARS). Define another variable that points to a directory containing the third-party source code (JAVA_SRC_DIR). You can set this up how you like, but we have a structure like this:
common/
lib/
java/ <-- JAVA_LIB_DIR variable points to this directory
axis/
bitronix/
1.0/bitronix.jar "extension" is "bitronix/1.0/bitronix.jar"
...
In your project's build path, use the "Add Variable..." option to add the library. Then you when "attach source," you'll be prompted for a variable and extension to the source code.
This way, a single, shared .classpath file can be checked-in, while allowing each developer to locate their own library and source directories where they like.
Related
After developing a java app in Eclipse, I would like to deploy it by packaging it into a runnable .jar with only the minimum necessary by the main method and its dependencies.
I have several packages in my workspace I work with too, but do not need to be in the resulting .jar file.
When I try to export, it clearly says that the required libs will be there, but also the other independent packages are inside too. (With the Export option happen exactly the same)
I choose to export only the Main class of the com.project... package, but also the test.project... has been packaged.
In the project I have both com... and test... packages obviously.
How could I force it to truly package only the required ones?
Thanks in advance.
TEMPORARY ANSWER (2019-07-03):
Seems that, for now, there is no way to achieve this automatically, thus the answer from #arnonuem seems a good workaround.
If better news, please feel free to improve this thread.
Thank you all.
I would create an ANT file for this specific task. There you can freely customize which packages should be compiled into the jar and which not.
Please inspire yourself reading this example.
For a general overview what i am talking about you could take a quick look into this.
https://howtodoinjava.com/ant/ant-build-jar-file-example/
Please focus on
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath"
includes="src/path/to/TheClassToBeIncluded.java" />
For more detailed information on how to include or exclude files or packages you should refer to the documentation over here:
https://ant.apache.org/manual/Tasks/javac.html
At the bottom of this page there is a list with valid build parameters.
First of all, we have to distinguish build and export.
Build transfers .java source code into .class byte code and mostly copies other resources. While doing that it usually merges all source folders into a single bin folder.
Export runs a build (or relies on Build Automatically) and then modifies the resulting .class files and other resources. Usually it packages them into one or more .jar files.
Therefore, our solution includes two steps:
build everything that is unwanted for export into a separate folder (or more)
export from a specific folder (or more)
For step 1, refer to this answer on Stack Overflow.
In a nutshell: Go to Project Properties > Java Build Path > Source tab > Allow output folder for source folders. This enables you to configure a specific output folder for each source folder in the centered viewer.
For step 2, we need to understand that eclipse's Runnable Jar File Export relies on a Launch Configuration.
So before exporting, go to Run > Run Configurations..., select the Classpath tab, remove the default User Entries and hit Advanced.... Now you can Add Folders containing your built classes.
You might want to use separate Run Configurations for internal testing and exporting.
I have struggled with this problem on and off for years, supposing it was just me who was failing to find the right solution. Possible solutions always seem to involve detailed manual configuration e.g. configuration of the build path, or selection of the folders from which classes are exported into a jar, or learning ant, but which still requires manual configuration. The problem is that the inter-dependencies between classes (and packages) are complex - imagine drawing a network diagram from import statements. Manual configuration is time-consuming, error-prone and, I think, infeasible except in simple cases. I am a bit stunned. If there is no automatic solution for selecting necessary classes, I suppose people are regularly exporting their entire code base and that the world is full of bloated jars ... (and, incidentally, without obfuscation, the entire source code base is thereby made available through reverse engineering).
I am working on a java project which consumes an external jar from another project. This external jar cannot be checked-in in lib folder as it evolves continuously. So we have planned to keep the latest jar inside a folder,say 'ExtJar', under User's Home dir.
The question is how do I modify classpath to point to this jar from home dir?
So, I was looking for something like and should work for all OS platforms-
classpathentry kind="lib" path="MyHomeDir/ExtJar/myExternalJar.jar"
where MyHomeDir is a variable I defined as per the link -
- Use Eclipse classpath variable to replace absolute "sourcepath" path?
I looked into above link to add a variable but apart from this I also want this variable to point to appropriate home dir depending on OS, i.e C:\Users\\ExtJar\ for Windows OR /home//ExtJar for linux etc.
Is there any way to programatically modify value of this variable to point to home dir after evaluating which OS its being run on?
The point of a Classpath Variable is that it's value is not the same for all workspaces; each workspace defines where the variable points to.
You can still use a Classpath Variable to solve your problem, though. For example, create a variable called EXT_JAR_HOME and point it to your C:\Users\your.name\ExtJar folder. Then in the project build path, use Add Variable... to select and **Extend...* it, selecting the actual JAR file. That will result in the project's build path having an entry like EXT_JAR_HOME/ExternalJar.jar. Then each developer workspace will just need to, one time, defineEXT_JAR_HOME` and point it to the correct path. Linux users' actual location will look different than Windows users, obviously.
The point is, Classpath Variables must be defined in each workspace, that's how they're designed to work.
I cannot find the address where my project's files are stored. Can you tell me where to find them as i would like to copy them onto my pendrive so as to run them on a different machine.
If you are using Windows, by default Netbeans stores the projects within your documents folder: \Documents\NetBeansProjects. If memory serves, on Linux they are within the ~/Netbeans Projects directory.
Alternatively, you can view your project's file structure by using the Files view, available on your left hand side (if not, you can activate it by going to Window -> Files.
Once that you have accessed this view, you can right click on any one of the nodes and select Open Project of Folder (if you ask me it should be the other way round, but anyways).
I have a java source file in my project, that I want to move to an entirely different location than the rest of the files (my unix home dir), but I still want to be able to properly use it in my project. Is there a way to do this?
EDIT - I see there is a vote to close this for being unclear, so - let's say you have a pJava project in Eclispe. All the source files are neatly saved in their relevant packages, under the same directory. Now, I want to move one of the files to somewhere completely different, but still have it used in my project. I hope this clarifies
Thanks!
I suggest that it depends on what your reason is for moving the source file.
The Java file is still conceptually part of this project, but you're moving it for general organisational purposes. In which case, the new "completely different" directory is another place where sources should be read from, which most IDEs will call a "source root". You should configure your project to read sources from there as appropriate for your IDE.
You're moving the file because it's distinct from this project. In which case I would suggest it ought to be a separate project in its own right. In order to still use the logic in your original project, you'd build the new project into a JAR, and then bring in the JAR as a library dependency (either directly, or using some dependency management system such as Ivy/Maven/etc.). Again, the details will depend on what your current setup is.
Right click on your Eclipse's project -> properties -> java build path , and under the source tab click Link source then choose the parent folder of your java file .
Is there a way to create user library in eclipse using class path variable to add relative path jars to the library
It's easy. Don't use User Libraries. Instead, use Variables. Example from my workspace:
Open image in a new tab to see full size
Further reference: Here and there
The approach suggested by GGrec is functional and I used it. But "User Libraries" allow some sort of grouping/structuring, for example "Jersey", or "Hibernate", etc.
I found an alternative way to get it working: Created the simplest Eclipse Project. Then inside it created "Linked Resource" Folder using an Eclipse Path Variable. Now I can create the necessary "User Libraries" and "Add JARs" to them, using this Linked resource folder.
In case the location of the jars is changed, the only thing that should be altered is the Eclipse Path Variable, pointing to them.
May be this approach can help someone else.