Recently i have been playing around with writing plugins which i then load using a ServiceLoader. In order for the ServiceLoader to find my Plugins, there has to be a text file included in my jar under META-INF/services/. The text file has to be named after the Interface the ServiceLoader is "looking for" and must include the canonical name of the implementing class(es). All of this is working absolutely fine, but there is one really annoying problem with this approach: each time the project is built, i have to manually add the /services/ folder as well the text file to the jar (using Winrar). This is extremely cumbersome during development, since i have to constantly re-built the project in order to test things, which means i also have to add the folder and the text file by hand every single time as well.
I really hope there is a way to have Java add the META-INF/services/ folder as well as the text file automatically when building the project, since this is driving me insane! :)
By the way, i am using Netbeans which (afaik) is using Ant internally to automate the build process. I guess this information could be relevant to my question.
Thanks!
Right click on your "Source Package", select "New | Other"
From the "Categories" tree, select "Other". From the "File Types" select "Folder"
Name thy folder...
Viola :D
Now right click on the new folder and follow the same basic steps, but this time, use "Empty File" instead to create your text file
Related
I know there are many related questions, but after reading at least 5 of them, I still feel many questions remain unanswered.
This jar file is a generic argument parser for java programs, such that you will use less time writing arg parsers for each program, and instead use this. Although the program is well documented, a programmer like me who still has not used external .jar files, finds it odd that there is no documentation whatsoever about how I should import this(what classes etc).
So I know that I need to put the .jar file somehow in my classpath. And then probably run some additional commands when compiling my program. But how do I find out what to import from the .jar file?
Thanks in advance, and bear with me. I am an import-noob.
Importing classes
importing the classes is symply done by import package.subpackage.Classname;
Using libraries in Eclipse:
To add the .jar file to your libraries, you better use a proper IDE (interpreting your question your are using a texteditor). I recommend Eclipse.
Create a folder in your projects folder in the workspace called "lib"
(or anything that suggests it contains your libraries)
Move the .jar file inside the "lib" folder
Right click on your Java project and
select "Preferences"
Select "Java Build Path" in the appearing window (menu on the left)
Go to "Libraries" (menu on the top)
Select "Add JARS..." and select your .jar from the "lib" folder
You should now see the library listed under "Jars and class folders on the build path" Hit "Apply" and "Ok".
You need to import the classes you want to use just like any other program. If you want to know which classes you need to read the documentation or use you IDE to add the imports for you.
If you were to cut and paste the program into an IDE it would be able to work out the imports for you however, if you don't have an IDE you need to read the javadoc http://www.martiansoftware.com/jsap/doc/javadoc/index.html which lists the packages you need for each class.
Adding a JAR is the same for every JAR so I guess they assumed this knowledge is covered elsewhere. When writing documentation, some knowledge has to be assumed or you would have to start with step 1) turn on your computer 2) wait for it to boot up ....
I have been trying to figure this one out for a bit, not coming up with the right approach. I read through this question, which I'd already tried, but it's not working quite right.. using IntelliJ EAP 142.4675.3
What I am trying to do, is to debug a junit test using a jar (extending the Provider class) file I've placed into JAVA_HOME\jre\lib\ext. I want that jar to be debuggable as well.
The problem is, if I attach my source directory as the source for a jar, then, in a stack trace, when I click the named file (containing a method I wish to view), IntelliJ asks me to choose between two of the same file, presumably because it knows about the source (it's in a module in my project) and it's also been told about the source via the jar source path I added.
I tried adding the provider.jar to the project module dependencies tab, which caused intellij no end of grief. Reverted back to having it in jre\lib\ext, and, in the Platform Settings->SDKs->1.8->Sourcepath tab, I added the module sourcepath. This lets intellij step into the .java file for the jar, vs the .class file, but, when it does so, I now get a banner at the top of my code windows which says "Alternative source available for the class xxx" and there's a combobox at the RHS with the module name listed twice (in the latest EAP, it actually lists the module and the jar [same name as module, with .jar]). And, as mentioned earlier, the IDE asks which which (of the very same) file I wish to edit when I click a file in the stack trace.
Clearly, there is something not quite right.. what am I missing? I find it odd that the ide will not open the source code when stepping into the jar until I attach the code as above, but, when I do, it sees it twice.
So let's start from scratch:
File > New Project, choose "Java Module", fill the form:
Bypass the form asking about the kind of project (webapp etc) by clicking on "finish"
you now have a project with a src dir:
right click the 32910506 directory and choose "New" then "Directory" and create a new directory named lib.
drag and drop your 2 jars (sources and code) to this directory (I will use common-lang for this example), beware to copy, not move:
now right click on src, choose "New", "Java class", name it App:
right click your "code jar" and choose "Add as library":
in App type psvm then hit TAB to get a main and fill it like this:
right click on your "code jar" and choose "Open library settings":
click "+" and choose "Attach file or directory" and select your "sources jar":
Now if your Ctrl+click on random in App, you should end up to the method source:
Put a break point in the random method:
Now right click App, choose "Debug App.main()", you should end up to the previous breakpoint:
Used: idea 12.1.6 under linux
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 .
I have a Java JAR file that was developed to run in the browser. It works fine. There doesn't appear to be any sort of build file associated with the source code I've been given and I'm attempting to create a project and/or build script.
The source code references some third party code. What I've done is unzipped the JAR file into a folder that has this structure:
\App\src\com
\App\src\META-INF
\App\src\applet
The "com" folder contains subfolders which contain the .CLASS files for the third party libraries. "applet" contains the .JAVA source code files (one folder, maybe 15 files).
The code appears to be written in 1.4 syntax (I manually attempted to compile some of the .JAVA files and it complained that some things were deprecated and/or required the older version).
I created a Java Project in Eclipse and pointed it to the \App folder. Then I went into the Properties for the project and went to Libraries and and clicked on "Add Class Folder" and added the \App\src\com folder. Then I went to "Order and Export" and moved this to the top. I also set the Java Compiler version to 1.4.
I am getting a large number of compiler errors, all of which seem to stem from the fact that it says it can't resolve an import. This import is the set of .CLASS files contained in the "com" subfolders. The namespace matches the directory structure.
This seems like it should be really straightforward, but I've tried various things and don't seem to be making any progress. Based on what I've read I'm sure it has something to do with the pathing but I'm at a loss at this point on how to fix it.
(I'm sure it goes without saying that I'm not a Java dev.)
Any thoughts or ideas as to where I'm going wrong?
Can you please try to right click on Eclipse's Navigator. Select Import>General>archive file> (Jar file that you want to import).
You should be getting the jar file correctly imported at this point. Do tell me if you face any problems.
I'm currently developing a 2 project system, mainProj and sideProj.
The idea would be to have the output .class files from sideProj be put as mainProj's resource files, so I'd like to find a way to automatically put the output os sideProj as resources of mainProj.
The "default output folder" option only seems to allow me to output to any directory inside a given project, not to other projects.
What would be the best way to accomplish this (preferably without having to resort to ant)?
Isn't it easier the other way around by simply adding sideProj to the build path of mainProj? Like for every normal library project and main project? Or is there a more specific reason for moving those class files?
If you really want to process as described initially, an Ant based builder is probably your only choice (select sideProj, context menu, properties, builders, add, Ant, ...). While you have to write that short script yourself, it takes part in the automatic build process afterwards without you needing to invoke it.