Duplication within Java classpath issue - java

I have a program that imports its required classes from a .jar source.
However I have also unzipped said .jar source in order to search its file directory to gather up class names for a list within the same program.
The problem is that the class ** appears more than once in the classpath, this is due to it being simultaneously in the bin and as a library. I need both elements for separate, yet equally code features.
I've found removing the file directory of the .jar source solves the problem however my list is now blank.
Anyone know of a way to do both?

Based on what you said above in the comments, if your package names start with com.somethingfromsourceforge.* then I would change com.somethingfromsourceforge to com.hopeless and the pathing issue should be resolved. Try this and let us know.

Related

No changes after I edit a jar file

Sorry if the title doesn't say what I'm trying to do very accurately but I wasn't really sure how to describe it in one sentence. Basically I have a jar file with a java file inside it, and I want to edit the java file. I used 7zip to do so and it worked, letting me edit the code in the java file. I saved it and my new code shows up when I open the java file in netbeans. However, the new code isn't actually in effect, and my program is still using the old code. I don't know if this matters, but the java file is named Templates.java, located in a package called net.sf.dynamicreports.examples in a jar named dynamicreports-examples-5.0.0-sources.jar.
Thanks in advance for any help anyone can offer.
Edit: tried importing the jar into a new project and editing from there before putting it back into the original project, but had no luck (check comment chain for further details). Still have no idea how to fix this and would greatly appreciate any help
Your jar contains .class files and .java files.
When you create the jar, the .java files are compiled and .class files are created from them. You should have a Templates.class file that corresponds to Templates.java.
When you edited Templates.java, Templates.class was never recompiled. This class file is the one your program uses.
The appropriate way to change a jar file is unfortunately to regenerate the entire jar.
So, what you should do is open the code you used to create the jar in the first place, make your java changes there, and create a new jar.
You will need to do this even with the smallest changes.
I didn't make the jar myself unfortunately, I got it from somewhere else so I don't have the code used to make it, just the jar itself. What should I do?
Edit: I saw your comment that you did not make the jar in the first place. This isn't too big a problem since you do have the java source code. What you need to do is copy all the .java files in the jar and make a new jar with them. Or, simply use Eclipse or whatever your favorite java editor is to compile your Templates class, and copy the new Templates.class file it creates into the appropriate place in the old jar, overwriting the old one.
Where would I find the Templates.class file?
Edit 2: You find the class file in the bin folder of your project if you are using Eclipse. See this question: Find the .class file compiled by Eclipse or if you are using a different editor/compiler, search google for where it stores its .class files.

how to work on a project that solely consists of .class files in eclipse?

it's been a while since i've been working with java and especially with eclipse. My professor sent me a huge folder with many subfolders and subsubfolders, that mainly contain .class java files. Now I'm supposed to work on these files, but i just can't seem to figure out how to get all of them working. I found a few solutions for single class files, but i have a whole folder hirarchy here that i want to work on.
I hope you can help me - I read something about decompiling? How does that work?
Note that I have around 50 different files here that need to be accessible.
Thank you very much!
I suppose the class you are attending is not something like "CS 902 - Reverse Engineering", because if that was the case, you would know what to do with the .class files.
So, one of the following holds true:
Your professor has made a mistake, and instead of sending you the java files, he/she sent you the class files instead.
Your professor sent you the entire project, which contains both .java and .class files, and for some reason you have only managed to find the .class files, while the .java files are there, and you just haven't found them. Unfortunately, the convention in the java world is to store .class files in a subfolder under the project root, so if you copy the project folder, you are copying .class files together with everything else.
Your assignment is to write new code which makes use of classes and interfaces supplied by your professor, but your professor does not want you to have the source code of those files. In this case, you can still work with the .class files, because the public definitions contained therein are parseable by Eclipse and usable in your project, without any reverse engineering. So, what you need to do is to find a way to tell eclipse that these .class files form a "Class Library" which is supposed to be used by your project, and then go ahead and develop new .java files making use of the library. I don't remember how this is done in Eclipse, but you should be able to find it out by yourself, or look it up, or perhaps someone else might post a how-to answer. However, at this point we do not even know whether this is in fact what you need to do.
You can use a java decompiler like JD-GUI, you can find it at http://jd.benow.ca/ . This is a very handy tool to have when you want to view a decompiled version of your jar. There are plugins available for eclipse and intellij as well.

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

Adding external .jar file in Eclipse

I'm having trouble adding a .jar file I downloaded for my Java project. This is really the first time I've used eclipse, so please bear with me and for some reason (I have no clue why), I just find it somewhat confusing.
I know that in order reference different class files you simply need to create a class library and add it to the build path. From there, all which needs to be done (unless I'm misunderstanding this for whatever reason) is use the "import" keyword to import whatever .jar, .java, or .class/.interface file necessary into the project.
I've tried that with my .jar. I have it referenced in the build path (all I did was just copy the jar to the project directory, and then use the build path option to add it externally), but when ever try to call the object "Delegator", which obviously is a part of the .jar file, it won't read.
Am I missing something here? Seriously, anyone who knows the answer to this - you're relieving a mother of a headache. And before anyone asks - yes, I've searched this one to death. I've found similar questions, but nothing which quite hit what I was looking for. Either that, or I really just lack the common sense.
Right click on project->BuildPath->Libraries->Addexternaljar and then press ok and if it doesnot worked then you should go to the Order and Export tab and checked the jar you have just added in your project. It will solved your problem.
There are several possible reasons, for the question hasn't mentioned the specific failure, and where it has occurred. The following is a list of possible reasons I could think of, but this may not be exhaustive:
You can import a class, in a different package only if the class is public. The only exception is when you are using the class in the same package. If the class is an inner class marked as private, then you're well and truly out of luck. The Delegator class in question might not be public, and that's why you may be unable to use it. This issue ought to be caught by the compiler.
The directory structure within the JAR might not match your package import statements in your classes. This might not be necessary, for Eclipse ought to provide possible fixes, but it is better to verify that nevertheless. Again, the compiler should complain if this is the case.
If the issue is at runtime, then, it is most likely that the JAR is not available in the runtime classpath. You'll need to configure the Runtime configuration, to add the JAR to the runtime classpath. Refer to the Eclipse documentation on run configurations, if you need to know how to change the runtime classpath.
Note:
Exporting the build classpath entries would matter to other projects that depend on the pertinent project; unexported entries will have to be re-imported if required in other projects. This would not apply to a run configuration.
Update
Every Java application needs a main(String[] args] method to start execution. This is the entrypoint for the application. From the comment, it appears that the main method is in a different class. If so, the said class ought to be used to start the application. In Eclipse, a "Run configuration" might be used for the class that lacks this entrypoint, resulting in the described error. One can rectify this by creating a new Run configuration for the class with the said entrypoint. This may be done by one of the following:
editing the existing Run configuration to use the desired Class (the one with the main method). See the above link, in the third bullet point. Edit the value of the class to be launched.
creating a new Run configuration for the desired Class. Usually, you'll need to traverse to the desired class, and run your application (using the Alt+Shift+X+J shortcut) from the said class.
i was facing similar issue with spring jar files but then tried with different jar files and it work so I think , classes defined in jar files were private and not available outside of jar hence you were not able to access the file .
thanks ,
Raju Rathi
Right click on the project--->Build Path--->Configure Build Path...--->In left side you have to choose Java Build Path--->Libraries--->Add External JARs--->ok--->ok
Steps to add jar file in eclipse
1. right click on project
2. click on Bulid Path->configure path
3. click on java Build path
4. Click on libraries tab
5. click on add external jar tab
6. choose jar file
7 click on ok
Copy the .jar file in libs folder which you want to add in your project.
Right click on .jar file -> Add Build Path
Done.

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...

Categories