Now that I've learned a bit more, I can ask a more direct question.
Scenario:
I have a Java project in IntelliJ Idea, with the end goal of making a standalone .jar capable of running on Windows or Linux, either run from the command line with java -jar jarname.jar or simply by being double-clicked like any normal, simple jar.
I've written a handful of classes, located in my src/package/name directory. However, one of my classes requires the use of an external class, i.e. a class not located in my source directory or is not part of Java's default set of .jar's in the JDK.
How do I go about configuring IntelliJ to build the .jar artifact AND include the necessary resources inside of it, with everything needed put in the proper place, so that my class can use the resource by an ordinary import statement? An answer given in example form would be awesome; I've almost figured it out, but there must be just one thing I'm not doing correctly.
Thanks for your time,
Yankee
Sources:
http://tukaani.org/xz/java.html
Viewing the directory structure of that source gives a better idea:
http://git.tukaani.org/?p=xz-java.git;a=tree
One does not simply package Mordor into their jar.
After much experimentation, I found a solution that, while maybe not the right way to do it, definitely works. The key is to:
Define your external library (a .jar in my case) as a module dependency.
Add your external resources as (what IntelliJ calls) an "Extracted Directory."
For the first item, go to File -> Project Structure. Click "Modules" in the Project Settings list on the left. In the list just to the right, you will see a list of modules (whatever they are) which belong to your project. Leave that alone, but make sure that it is highlighted/selected as the current module. What you want is the settings for that module, which will show in the window on the right. Go to the "Dependencies" tab. On the rightmost part of the screen will be a little green plus sign. Click that, choose "Jars or directories" and navigate to your relevant resource you want to bring along with your finished jar.
Note: don't be fooled by the check box that says "Export." Its only purpose is to cause endless pain and suffering as you wonder why the dependency isn't exported along with your jar. Always remember, the export box is trying to get you to click on it. It wants to be ticked :3
Next, add that resource as an extracted directory by going to File -> Project Structure. Then "Artifacts" in the Project Settings list on the left. Click the green plus sign at the top to make a new "Jar" artifact "From modules with dependencies." In the right hand window, under the "Output Layout" tab, click the little green plus sign and choose "Extracted Directory." Once again, browse to your precious jar.
You should now be able to successfully build a jar artifact that contains all the resources it needs to run as you designed it.
Here is a reference section on configuring external libraries as module dependencies - link. Regarding artifacts construction - it's possible to precisely specify its content (including dependency libraries content) - link.
Read How classes are found...
It is OK (expected that you) to ship your program in your own jar, and dependent 'libraries' as separate jars
You are expected to provide a way to run your program with the correct -classpath argument to the java command so that java can find both your jar, and the dependent jars... there are multiple ways to do that... see the link above.
Rolf
Related
(Thank you #MarkRotteveel !)
I'm learning Java. I want to import class of another project. 'a' class('MySoup') in project A('recipe_nomodule') to project B('test')
However, there is no JRE System Library and it doesn't work. And I can't find posts about this problem
ㄴ 1. I created another project. (recipe_nomodule)
and a package(recipe_nomodule) and also a class(MySoup)
ㄴ 2. I created recipe.jar
ㄴ 3. Java Build Path - Libraries - Add External JARs... (in Properties)
ㄴ 4. result.
"MySoup cannot be resolved to a type"
"The type recipe_nomodule.MySoup is not accessible"
(23/01/2023 Thank you for the answers)
In the 'build path' settings of your new project (as per the screenshot you posted for item 3), add the other project in the 'project...' tab.
You don't need to make a jar, you don't need to add that jar via 'external libraries..'. You don't want to do that - you want the project dependency. That way, you can edit a file in either project, just save the file, and run, and see the updates, without having to go through the routine of making another jar. In debug mode, any change that doesn't affect signatures will even be instantly applied, no need to restart (only applies to long-running apps, of course).
Why doesn't your current approach work? Your question doesn't include enough information to be sure. A few options, based on the fact that key error message is 'not accessible':
You've explicitly told eclipse you don't want the package recipe_nomodule to be considered accessible by anything except the project that created it. I doubt you've done this.
It's set up as a module-based jar (with a module-info file), in which case, you'd have to export the package in the module-info.java file.
The class wasn't marked public at the time you made the jar file, even if it is now (this gets back to: Don't go via jar files, that way any updates won't propagate and that's very annoying).
More generally if you want to ship these concepts completely separately, you need to start answering questions about how you'd like your jar file to be distributed, you need to sign up with sonatype/mavencentral or some other entity that distributes open source, you need to learn maven, and more. Probably not worth getting into that, especially given that 'for fun' / 'for exercise' projects wouldn't be accepted.
Situation::
Trying to search and open up a Java file. In a nest project.
And so, we have the file here, these 2 files the exactly same file,
but, when the project is nested inside, Eclipse search opens up the file that has no syntax highlight or source code link (ie: unable to ctrl click on the code).
(ie: Eclipse opens up the Java file as if the Java file is not on a build path)
(though, if I search directly on that nested project, instead of on the parent project, then things are fine)
I have to manually open up the file in the Project Explorer, to get the file with syntax highlight or source code link.
Ask::
How can I let Eclipse directly open up the Java file with syntax highlight (when I search on the parent project)
(so that I dont need to manually search & click all the way through)?
You seems to be using Maven and you probably imported your project in Eclipse as Maven project.
The problem with Maven and Eclipse is that usually Maven project are split as project aggregator (a pom.xml with only <module>) which m2e will import: such aggregator project use a directory hierarchy.
Eclipse, on the other hand, does not scan files as file system file, but as project related file: that's the main reason for your duplicate. The child project being contained in its parent, the same file is shown twice because for Eclipse, these are two distinct path.
And since for the parent project, the file is simply a regular file it does not offer anything beside syntax highlighting.
You can:
close each aggregator project one by one; for Eclipse, there are no really useful anyway.
don't import them (and I wish m2e-core would suggest it).
You could also create a bug or vote for this one which seems related: https://bugs.eclipse.org/bugs/show_bug.cgi?id=533440
Note that this also affect other screen such as Show Resources (which is the subject of bug 533440).
The workaround I am using::
workaround 01
Find the files in the nest projects as I did before, then get the class name, then ctrl shift t to search that class again.
Cons:
You have to manually copy and search the class name.
There are possible other classes with same name (and you have to tell which one you want to go to).
You will not be able to jump to the desire line.
workaround 02
Just search in the whole workspace. And directly open in the file in the child-project (ignore the results from parent-project).
Cons:
Time consuming when search the whole workspace (though, you can terminate it when you get the desire files, hopefully no missing results when you do that).
There are a lot duplicate / unnecessary results.
You may not want to search the whole workspace.
I'm posting this query after doing lots of googling and trying out different options for last few days. Is there an easy way to attach source in Eclipse? is the closest answer I found to my question.
From above post, I tried all solutions but each time Eclipse is opening .class file instead of .java file.
Eclipse Details:
Kepler Service Release 2
Build id:20140224-0627
Please let me know if this is a bug with Eclipse or if I am missing something.
If you are using Maven, try this:
Double click on your project in eclipse -> Maven -> Enable Workspace Resolution
This is most likely because Eclipse actually use the class file for declaring the item you have requested the declaration for, and not the source file you think it should be using.
The typical reason for this is if you have a jar file containing previously compiled classes on your build path before (or instead) of the project containing the source you want. This is very rarely desirable as it can take quite a while to discover that your edits do not take effect.
Examine your build path carefully and ensure it is as you want it to be.
Maven is likely supplying a Classpath Container to your project, and generating entries for the Java Build Path based on the pom.xml contents (the JRE System Library is another example of this). Its entries will not have source attachment unless the container decides it will, which is why the Installed JREs preference page offers to let you set Source Attachments. If the pom.xml has something like that, see if you can use it. If not, see if you can configure the Maven container from its context menu. Otherwise, you may simply be stuck.
I had this same problem: when I pressed F3 or tried to open Declaration (after right click) for any element, it took me to the bytecode .class file.
My solution:
I went to the project in Project Explorer:
right click on project name->Properties
Then in Porject Properties window,
Java Build Path->"Order and Export" tab
There I selected the folder with the .java files (source code), which was below the folder having the .class files -in my case "JRE System Library [java-11-openjdk-amd64]", and pressed 'Top' button on the right frame. Then the folder with source files moved to the top.
Finally, pressed "Apply and Close" button.
That solved my problem forever and ever.
The folder
I installed a plug-in which allows me to download the sources for a maven project.
I'm not by my develop machine right now, but I think it was this:
http://marketplace.eclipse.org/content/m2e-dynamic-sources-lookup#.U8gjGWIaySM
With this, you can select a project an have maven download the sources and make then available when you navigate the code in Eclipse. All without "leaving" Eclipse.
Check into your project modules the Java Build Path, look inside the Source tab and verify if you have "Allow output folders for source folders" checkbox flagged; in this case you just need to uncheck it and you will be able to see again the right source from java files.
This is related to the Call Hierarchy in Eclipse IDE.
If you are in a situation, where you're seeing duplicate methods inside the call hierarchy showing Members calling 'myMethod(...)', double-clicking on one of the listed methods may open the .class file instead of the .java file.
When this happens, the solution is to set the search scope in the call hierarchy to Project instead of Workspace.
After setting the search scope, you won't be seeing duplicate methods listed in the call hierarchy, and double-clicking on the listed methods will only open the .java files.
(Using Eclipse DevStyle Dark Theme)
I've been tasked with picking up someone elses Java code and adding some functionality to it.
I'ved pull down the source tree from CVS and see a bunch of .jar files in different folders. I'm guessing the developer did not use Eclipse.
I am new to Java (coming from .NET background) and have used Eclipse so far to create one Java project. I'm wondering now that I have this guys files (he has classpath.jar and other .jar files along with some subfolders each with 'java' files in them), how do I open them? I tried opening one at a time, etc.. but doesn't seem to work. IS tehre an easy way to do this? I thought there' might be some kind of 'import existing code' thing in Eclipse but I can find it. How can I do this? Do I re-create the folder structure and just add the existing files one a time?
Thanks much for any help
something like 'create project from existing source'?
http://www.stanford.edu/class/cs108/JavaTools/eclipse-guide/
if the existing code is not structured well, you are either going to have to heavily configure your project sources, or just change the project structure.
File -> new
Than select general->folder.
To make developing easier in eclipse i recommend some refactoring to the project.
create a new eclipse project using the parent folder as the home.
every folder that's the root of a hierarchy of java classes becomes a folder in the "source" tab (either on creation, or add through "project->properties").
every jar (at least the ones he's using, there may be extras) gets added in the project->properties libraries tab.
This is assuming that all of the hierarchies belong together and that the thing isn't structured to build little sub-projects out of pieces of the hierarchy. If there's a build file for this thing you might want to be sure that if the build file is doing that you're building things appropriately.
File->Import->General->Existing Projects into Workspace
OR
File->New->Java Project
This will create a sample java project for you. You can add the files appropriately.
Below is an example of a j2ee Project:
http://java.sun.com/blueprints/code/projectconventions.html
If C:\Workspace is the folder you are using as the workspace and you have your existing project placed as "C:\Workspace\ExistingProject"
Open Eclipse Got to File->New Project
Select the type of Project you want to create Use the name as "ExistingProject" for the project and click Finish or complete the remaining steps of project creation wizard normally.
Internally a .project file would be created in the ExistingProject folder and a .metadata folder would be generated under Workspace folder.
If you want to place the ExistingProject not under the workspace u follow the same steps.
There's 2 possibilities:
Import project from file system:
Create a blank Eclipse Project.
Then select File -> Import -> General -> File System. Select project, and point it to your created project.
Importing from CVS
Goto File -> Import -> CVS -> Project from CVS
Enter your CVS Host, Repository Path, Username and Password, and click next,....
Add what you need and click next (follow the instructions) until you're satisfied and click finish.
Hope this helps.
A simple tutorial that incorporates my 1st option and commmits it to CVS: http://thedesignspace.net/MT2archives/000662.html
Two options:
Maven - highly recommended but rather read this: http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html than have me re-write it here. Maven might seem like more effort up front but it pays for itself a hundred times over during the course of even a simple project.
Do it yourself (assuming Helios):
Move the source code Java files to ~/development/MyProject/src/java. Move the jars to ~/development/MyProject/resources.
In Eclipse, File > New > Java Project. Type in your project name.
Untick "Use default location" and browse to ~/development/MyProject.
Select src/java as your source folder (if Eclipse doesn't pick it up automatically).
Finish.
Then, for each error, you will need to find the corresponding JAR and add it as a library to your classpath in the project properties.
The important thing to bear in mind is that Eclipse is not like Visual Studio - you cannot easily just edit one file at a time and that is not what it is designed for. People can get frustrated with Eclipse after working with VS but if you just allow it to do things the way it wants you, your life will be much easier.
trick is finding the root folder. Generally, developers use the following:
project root
-- src
-- bin
at least, what's what Eclipse does by default. There are other ways it can be organized as Maven uses the following:
project root
-- src
-- -- main
-- -- -- java
etc...
More info on how Maven standardizes here:
That said, finding out how the source is organized shouldn't be too hard. Open up one of the .java files and look for the line at the top that starts with "package ". Should be something like this:
package com.somecompany.client.utils
Note, that's just an example, it won't be that exactly although it should start with "package". The value after package represents the path that the file should be in relative to the root of the source folder.
source
folder/com/somecompany/client/utils
So, if you follow the default way that Eclipse organizes things, it should look look like this:
project root
-- src
-- -- com
-- -- -- somecompany
-- -- -- -- client
... etc
SO, as other people have mentioned, you'll import from existing filesystem, point to the folder at the project root. You may need to configure it to point to "src" folder as a source folder. You may also need to import those .jar files into your project as well.
Good luck
I have several somewhat separate programs, that conceptually can fit into a single project. However, I'm having trouble telling Eclipse to make several folders inside a project folder.
A simple form of the structure would be:
/UberProject
/UberProject/ProgramA/
/UberProject/ProgramA/com/pkg/NiftyReader.java
/UberProject/ProgramB/
/UberProject/ProgramB/com/pkg/NiftyWriter.java
That is, ProgramA and ProgramB are both projects (in fact, they're currently existing Java projects), which conceptually fit into UberProject.
I don't think I'm supposed to make UberProject be a Java project; it's not a classpath, for instance. ProgramA and ProgramB do seem like they should be Java projects (they might use different build dependencies as well), but I see no way in Eclipse 3.3 to create two folders under UberProject that are intended to contain Java code. I thought about adding a .project file to each of the two sub-projects, but I'm not sure that's appropriate, either. Eclipse help isn't being helpful, and I didn't see anything on SO about this specific problem.
Just to be clear: assume as given the necessity of the existence of UberProject. UberProject can be a Java project, or not; it doesn't matter. (Incidentally, it does contain other folders that do not contain Java code.)
There are probably several ways to do this:
1) UberProject is your JavaProject. Right click ProgramA -> Build Path -> Use as source folder. Right click ProgramB -> Build Path -> Use as source folder. Both ProgramA and ProgramB will do incremental builds to the same directory.
2) Two java projects (ProgramA and ProgramB). You can use UberProject as your eclipse workspace which would be easiest or you can use an outside workspace and import ProgramA and ProgramB as external projects.
There are probably other ways as well (maven multi-module project). Your choice probably depends on whether you have cyclic dependencies between projects. It should be relatively easy to try both 1 and 2 and see what works best for you.
You can have multiple source directories in a single project, but your description makes it sound like you want multiple sub-projects in a project, which eclipse doesn't allow (as far as I know).
If you really just want multiple source directories, where ProgramA, ProgramB, etc. contain only java files and nothing else, then you can do this relatively easy. Right-click on your project in the package explorer, and select Build Path -> Configure Build Path.... In the dialog that pops up, make sure Java Build Path is selected in the left pane, click the Source tab, then click the Add Folder... button. Pick ProgramA from the list, and repeat for ProgramB, etc.
If you want each source folder to have its own output folder, just check the Allow output folders for source folders checkbox and edit the output folders as desired.
If that is not, in fact, what you want to do, then another option might be to group your projects into a working set. A working set is not an UberProject, but it does help keep your projects organized in the package explorer.
Do you need UberProject? I have the same layout but have multiple top-level projects created with File|New project. If not, can you make it a General rather than Java project?
So you can do it via having two Java projects in your workspace.
Then the question is how to group the two projects together under "UberProject"
One way is to have an "UberProject" workspace, and switch workspaces between UberProjects.
An alternative is to define "UberProject" as a working set (Window:Working Sets) and add PrmgramA and ProgramB as projects of that working set. Select that working set, and you see only those projects.
You can have one java project, and define multiple source folders for it. That is normally do that for "main" vs "test" hierarchies within the same project.
There are ways, and ways. Pick one that works for you :-)