Java class finder Tool - java

Anyone who uses Java regularly knows the amount of trouble multiple jars in classpath can give us. I am looking for a tool which will help me -
Search for class files inside jars in Classpath
Search for class files inside jars in a specific location
Maybe too much to ask for but show me if the same class is present in multiple jars.
Also needless to say it should be fast. Most of the tools i have evaluated are very slow and not upto the mark.
Any tools for the above purpose greatly appreciated.
Currently i use WinRar achieve search(which BTW is really fast) to search for class files inside jars in a specific location. But there is no way to utilize that to search in Classpath.

If you use Bash this script may be helpful:
find . -name *.jar -exec bash -c "echo {} && jar tvf {} | grep MyClass" \;

If you are using eclipse, then IBM's classfinder may be useful. You can perform a (fuzzy) search for a class in a (set of) directories, and get all jar that contains such a class.
Not sure about the classpath option, however.
regards,
Guillaume

You can use jarbrowser for this

If you're using Eclipse, and they're on your classpath, you can CapitalCaseControlSpace what you want.
For example, if I want to get a new ApacheFanUtilityMethodizerFunctor (which everyone needs, of course) I simply type AFUMF and then Ctrl+Space and it populates it for me. Since it's already on my classpath, I don't need to hunt for it, because it's there.
Now, if it wasn't on my classpath, and I had to find what jar it was in for purposes of including it, then back to WinRAR (or for me, Google typically) I go.

My own solution for managing java jars files and java classpath is really simple. Firstly I create folder inside e.g.:
C:\Program Files\Java\libraries\
Here I put all my *.jar files which i need to use.
Then I modify the:
PATH(modyfy also CLASSPATH or JAVA_HOME)
system variable and add this path shown above.
Problem occurs when one class is in two diffrent jars but I managed my jar to avoid this problem.
Now when java is running jvm can find my classes and ClassNoFoundException isn't thrown.

A late response, but in case anyone searching has a similar problem, I wrote a simple tool a few years back that I use all the time for this sort of problem.
It's free to use and open source. If you find it useful, let me know, :-)
https://github.com/eurozulu/Findclass

Related

Java main class autocomplete

If I'm trying to run a java program and I don't know the exact name of the Main class, is there any way to use tab completion to figure it out?
java -cp stackoverflow.jar org.<tab>
stackoverflow serverfault stackexchange
java -cp stackoverflow.jjar org.stackoverflow.<tab>
Main IntegrationTest QuestionAnswerConsole
Something along those lines.
Basically you are asking how to configure shell autocompetion to support java. It is possible. Take a look on this discussion: How does bash tab completion work?
I have to say that this is a good idea not only for discovering the main class but also to complete other command line options and a class path. I'd be glad to use such script if you develop it. Good luck.
EDIT
At least on my Ubuntu file less /usr/share/bash-completion/completions/java exists and therefore some completion should work. You are always welcome to improve the script.
The only way to get the Tab Completion that you are talking about is to use a shell that is "Java aware" or a shell script that provides this feature for the java command. git has a similar feature, so I don't think it's completely impossible.
Edit:
According to this question on SU, it is possible to create an autocompletion script for the bash shell. Since the question on SU is slightly different than what you are asking, I don't see a lot of specific details that relate to this question. However, it looks like a good place to start.
I don't know any easy way to get the autocompletion of classes names if they are hidden in a jar file.
On the other hand, you can add a Manifest to your jar to make it auto-executable (ie., you just have to run java -cp ... -jar stackoverflow.jar) !
In the jar archive, add a META-INF folder, and inside that folder, create a MANIFEST.MF file that reads :
Main-Class: org.stackoverflow.Main
(or whatever your main class really is).
Here is some documentation : Setting an application's Entrypoint
Other answers suggest you solutions but as a sidenote, you don't need it if you provide maintainer of the jar file provides a manifest file with path META-INF/MANIFEST.MF. Then java automatically extracts main class from the manifest and you can run it this way:
java -jar stackoverflow.jar

Libraries to create jar from a Java file

I am trying to write a program in Java, which would collect a specified set of files from the physical location and place them in a jar in a certain directory.
I know I can use java.util.jar package for this. The package is great for sure, but I was wondering if there are any third party libraries that anyone at SO has used which they could recommend.
TL;DR: pick up files, create jar by placing them in pre-defined directories, all at run time in a java file.
P.S: I did not find any similar references to this question, but if there are then please refer me. Also, the title might be misleading, but I didn't find better words to explain my problem.
EDIT 1: I am not in anyway saying java.util.jar is bad or incomplete. All I am asking is if anyone has used any alternative solutions!
EDIT 2: I am trying to create the jar from inside the java program. The jar can be pretty big (~500mb) too. Trying to jar media files as well. So simply put, I pick up various files from certain locations on my drive and try to create a jar file by placing them in standard locations.
Thanks,
Ivar
Look at the java.util.zip package. Jars are just zip files with some extra stuff in 'em

How to update rt.jar file?

I want to add some class files to rt.jar. How am I able to do that?
Your question indicates you have some misunderstanding of the java platform.
First of all you need to know what the rt.jar is and what it does:
rt.jar is the jar that contains all the classes necessary for the java runtime. Hence it's name rt.jar
Now that you know that, you need to know how your java program runs:
Your java program, all your jars and classes are executed by the java virtual machine.
So as you can see the code you write & the rt.jar which is used by the java run time are completely separate and should remain so.
If you need some functionality you should add it to your jar.
Do not update it. Why do you want to update it?
Well anyhow if you want to update it, I know one way, You can open jar file in winrar and paste updated .class files in archive. But your jar may get in inconsistent state. Do it at your own risk.
You can use this command:
jar uf jar-file input-file(s)
Refer the link for details:
java.sun.com/docs/books/tutorial/deployment/jar/update.html
The best way to update the rt.jar is to install a newer version for Java. ;)
If you want to add your own classes in new packages, you can add these to a jar which is in your class path. This is preferable to changing existing classes.
If the only option is to change existing classes, you can create a "patch" in a jar which you prepend to your boot class path, or you can add the jar to an lib/endorsed directory. I wouldn't do this for production code, only for your own testing.
I've tried jar.exe with the u and 0 (i.e. zero) options and that gets the closest to looking like the original rt.jar file but if I've updated the JDK's JRE's rt.jar I have problems with compiling and jarring after the update. No idea why! Simply running a program with the JRE seems to work.
I also tried -Xbootclasspath/p but couldn't get it to work.
Looking at Replace a class within the Java class library with a custom version I see that there are legal problems with distributing an altered rt.jar to your customers, even if you could figure out how to do it correctly. So I plan to take the advice in that page and create a java agent. That's apparently legal and works.
One reason a person might want to modify rt.jar is to add debugging information after compiling the source that comes with the JDK with the -g option. One may also want to patch something. These would be for one's own use, of course.

lambdaj installation

we have downloaded jar files for lambdaj and its dependencies which are again jar files.
we do not know how to go about it. we have copied these files in the
C:\Program Files\Java\jre6\lib\ext
have set the class path in environment variales as:
variable: classpath
path: C:\Program Files\Java\jre6\lib\ext
but we do not know how to go further. we want to run some lambdaj programs.
can anyone suggest how to run lambdaj programs?
You would run a Java program that requires lambdaj in exactly the same way you'd run any other java program with an external dependency, i.e. by invoking the java executable passing in the fully-qualified name of the Main class, or the JAR with an appropriate manifest, or by deploying it in a servlet container, etc. Additionally you should be putting the LambdaJ JAR on the classpath for this invocation, not in the lib folder for your entire JVM.
What have you tried so far and why/how is it not working? Your question at the moment is a bit analogous to "I want to use Microsoft Word to view some Word documents, how do I do this?".
Update for comment 1: You said "it's not working". That doesn't help anyone address your problem as it gives no clue what you expected to happen and what you observed, only that they were different. As for where JAR files can be stored - you can put them in any directory, so long as that directory is on the classpath (or you add it to the classpath) of the Java application that runs. The canonical place to put external dependencies is in a folder called lib below the root of your project, but the important thing is that you choose somewhere consistent and sensible.
It sounds like you don't quite grok Java and classpaths yet. If you have followed some tutorials and are still stuck, ask for help to let you understand. Adding more detail to your question, including the layout of your files, the commands you issued, and the response that came back would be useful too.
If you are using Netbeans create a project and right click on the Libraries folder within the desired project. Click Add JAR/Folder...

Can I add classes to sun's rt.jar file?

I downloaded the Javax.mail package. I have jdk1.6.0_11.
Problem is...I cannot get javac or java to find those classes!
I can get apps to compile using JCreator LE ( by adding the mail jar
to its search list ) but, when I try to run the app in a command window,
it fails.
Can I add these new classes to the rt.jar without
hurting my jdk installation?
I know java has it wired up to look there for classes.
(And, the mail classes are inside a javax package - seems like
they could reasonably be added to the javax folder in rt.jar..
Thanks!
Phil D'
No you can't, nor should you.
Instead, figure out the problem with your classloader (probably paths?). You'll need that for the next library you need to access.
Messing with rt.jar means you can't run on any other JVM.
You should either specify the jar file in your classpath: preferably on the command line with the -cp option, but possibly with the CLASSPATH environment variable.
Alternatively, you can specify its directory in the java.ext.dirs system property. For more details, see the documentation for the extensions mechanism.
You shouldn't be messing around with rt.jar. That's very definitely not the way to make extra jar files available - it's akin to trying to add Microsoft Word to the Windows kernel ;)
Adding things to rt.jar seems like a bad idea, even though its possible and easy to accomplish.
Try compile your application from the command line like this:
javac -cp <path_to_3rd_libs>/jarfile.jar . MainClass.java
If the compiler still complains about the javax.mail package try to unpack/examine the jar file to see that javax.mail package (and its expected content) is there.
(On windows its easy to examine a jar file using 7zip.)
Most definitely no.
If you post the command you are running from the command line we will be able to point you on the right direction, but most likely you are just missing a classpath parameter.
java -classpath /path/to/mail.jar MyClass
You need to understand the CLASSPATH concept which allows you to add individual classes and jar files containing classes to the "universe" of defined classes available for the code you want to compile and/or run. It is similar in idea to the PATH variable in the Windows world.
For the Windows command line this is the documentation:
http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html
The Java Tutorial surprised me by not being well-written for this particular concept:
http://java.sun.com/docs/books/tutorial/essential/environment/paths.html
You most likely need something along the lines:
C:> set CLASSPATH=c:\javamail\first.jar;c:\javamail\second.jar
after which both java and javac should know about these classes

Categories