Java NoSuchMethodError same class name - java

I have two .class file with the same name and same package in two different .jar file
First jar:
Second jar:
When i run the program from eclipse i haven't problem, eclipse use the first .class file (i must use the first . class, i don't need the second .class, i want exclude it).
When i export runnable .jar i saw that is executed the second .class file and then i have
NoSuchMethodError exception, because the second .class is different from the first.
How can i use always the first .class and exclude the second?
I don't need the second .class, but i need other class from his library.

Java loads classes from classpath that is defined dynamically when you are running application in eclipse and is controlled by property Class-Path in file MANIFEST.MF located under META-INF in you jar file.
So, first open jar file using any ZIP tool and take a look on manifest. Try to change the order of jar files into manifest and run again. I hope this will help.
BUT this is extremely bad that your alive-matchmarker.jar contains file that it should not contain. I do not know what library is it but is there a chance that they have other distribution that does not contain their own dependencies? Or probably try to find other version of this library. The worse thing that can be is if you have different versions of the same class in your classpath: the behavior of your application can be buggy and unpredictable as a result of this duplication because you can never know what version of class is used now.

Do not import the whole package like
import org.mindswap.*;
You can import specific class you want from any specific package like
import org.mindswap.wsdl.WSDLTranslator;

Related

How does java find .class files of java standard library or libraries I buy

I came across this question:
What is a classpath and how do I set it?
and the first answer explainns what classpath is and how to set it:
.....First, let's suppose that MyClass is something you built as part of
your project, and it is in a directory in your project called output.
The .class file would be at
output/org/javaguy/coolframework/MyClass.class (along with every other
file in that package). In order to get to that file, your path would
simply need to contain the folder 'output', not the whole package
structure, since your import statement provides all that information
to the VM.
Now let's suppose that you bundle CoolFramework up into a .jar file,
and put that CoolFramework.jar into a lib directory in your project.
You would now need to put lib/CoolFramework.jar into your classpath.
The VM will look inside the jar file for the org/javaguy/coolframework
part, and find your class.
So, classpaths contain:
JAR files, and Paths to the top of package hierarchies....
but if java only looks for classes in directories specified by CLASSPATH variable how does java find classes from libraries that are part of JRE?

import java package into a jython project in Eclipse or PyCharm

Using Eclipse + PyDev + jython. Need to import a java package to use a Java class inside a Python program (using Max OSX).
For import, I mean statement in Python like from com.a.b.c. Wondering where should I put the Java jar file which contains com.a.b.c? Thanks.
BTW, if any PyCharm + jython based solution, it will be also great. :)
This question is not duplicate from the other one, the other one's title is bit mis-leading, and that one is about how to install jython.
The import semantics do not differ that much from CPythons from what I see in the Jython Docs.
First a search is made for the .jar file in the current directory; if it is not found there, it is looked up in the directory containing the core Jython libraries. The classpath which corresponds to Javas CLASSPATH is then searched along with the site-packages directory containing external libaries. I am not yet sure what __pyclasspath__ is.
So if a package is not found in those directories an import error is raised. You have two options:
Either add the .jar in one of the directories (typically you should never add it to the directory containing the core libs.
Add the .jar to your CLASSPATH.
Add the path to your .jar in sys.path.
For the first case, either move it to the current direcory or in site-packages.
For the second case see here on how to add a .jar to your CLASSPATH.
For the third simply call sys.path.append("path_to_jar") to include the directory containing your .jar file to sys.path.

How to import .class file into a .java file in eclipse correctly?

I need to import a few class files into a java project in eclipse. I put the files in a folder and then add the folder to my project by right clicking the project, then select Properties > Java build path, then "Add External Class Folder".
Then I imported the class into the java file using an import statement:
import cla11.classname;
("cla11" is the folder name, "classname.class" is the name of the class file.)
However, the compiler doesn't allow the import (import cla11 cannot be resolved) and the classes contained in the class files are hence unusable in the project (... (class name) cannot be resolved to a type).
Note: I am aware that my question is almost the same as the one in this link: https://stackoverflow.com/questions/2477947/how-to-import-class-file-in-a-java-file#=
I used the method described in the answers, but the compiler does not allow it (as described).
(Since I am not yet allowed to comment, the only way I could think of is to ask the same question again.(Any other suggestions on what I could do to solve such problems would be welcome.))
Class' package name and your folder name must match. For example, if your class' full name is com.example.MyClass you need a directory structure like com/example/MyClass.class and import the root folder to the Eclipse.
Update
I do not know your actual needs. Like why do you even need to import an external .class file. So you may prefer to create a Jar file and add it as dependency. IMHO, This way it will be a lot easier to distribute your application.
You can create a jar file like this jar cf my-library.jar com/ after that, you can add Jar file as a dependency. For example if you are developing a web application you can simply drop your my-library.jar file to ${project-root}/WebContent/lib directory. Or if you developing a console application, you can simply add your my-library.jar file to your class-path.

Import user defined package in java from outside of the subfolder

I have created a package in c:\world and I want to import it to my java source file stored in d:\java. It says that unable to access the package c:\world\Balance.class. What do i need to do??
You'll need to have the location of the imported classes on your classpath when compiling your classes and running your code.
From your description and comment, it sounds like you have a package named world with a class named Balance, with Balance.class in c:\world. This should work from d:\java:
javac -cp %CLASSPATH%;c:\ SomeClass.java
Replace SomeClass.java with the name (one or more) of the classes that you're trying to compile in d:\java.
You'll also need to have c:\ in the classpath when you run your code.
If you are seeing what I think you are seeing (no c:\world in the specific error message), then it's easy. You need to add c:\world to your CLASSPATH setting.
After that, a simple import Balance (or whatever) should suffice.
Otherwise, a MWE (Minimal Working Example) illustrating the issue, and the exact error message you are getting, would be helpful.
You have 2 ways to solve this problem:
Make the packages available in a single project, this means, both packages would be in the same source directory.
Make a jar that contains your world package. Copy this jar to your lib folder in your project and add it to the classpath, now the world.Balance class can be reached in your current project.

How can I create a .jar file?

In the tutorial I found out that jar files can be created in the following way:
jar cf jar-file input-file(s)
However, it was not clear what are the input-file(s). Is that .java files or .class files? From the examples on the same page I can assume that should be .class files.
But now it is not clear which .class files should I put there. After the compilation of .java files I have a lot of .class files. One of the reason of that is that I have a lot files like that: GameWindow$2$10class, GameWindow$2$7.class and so on. Should I include all of them into the command line for the creation of the .jar file?
To run my application I use java Game. So, my be I need to use only Game.class file when I create a .jar file? On the other hand other class files corresponds to classes used by the application.
My software use external libraries (during compilation I specify .jar files of these libraries). Will .jar file of my application be able to run on the computer which does not contain the .jar file of used library?
However, it was not clear what are the input-file(s). Is that .java files or .class files? From the examples on the same page I can assume that should be .class files.
Yes, you need to include the class files.
I have a lot files like that: GameWindow$2$10class, GameWindow$2$7.class and so on. Should I include all of them into the command line for the creation of the .jar file?
Yes, these are from inner classes; you need them as well.
To run my application I use java Game. So, my be I need to use only Game.class file when I create a .jar file?
No, class Game will use other classes, which in turn use others. You need them all.
Will .jar file of my application be able to run on the computer which does not contain the .jar file of used library?
No.
That said, creating a JAR manually is a good learning experience, but not something you'd really do in practice.
You should probably look into automating you JAR building. Check out e.g. Ant: http://ant.apache.org/
You can use a wild card to add the classes in the current directory into the JAR-file.
jar cf mynewjar.jar *.class
When you compile your source file into byte code, all classes inside that source will be generated as separate .class files, so unless your game.java has more than the Game class, the game class would be sufficient.
The Jar-file could contain any file you want, but to hold a Java-program you need at least the .class-files, so you have to include them. The Game-class you are talking about may be dependant on the other classes. You can check that: delete all .class-files and recompile only Game.java (javac Game.java). All other classes that are compiled are a dependency. All of these have to be included in the Jar-file, that your program can be run. The class-files generated, that have not a corresponding .java-file (i.e. your GameWindow$2$7.class) are inner anonymous classes, in your example a inner class of the class GameWindow. If other libraries are needed, these must be present on other computers, that your program can be run. But you can include the content of the other jars into your jar, so that all that is needed is bundled into one file.
The .jar file must contain all your classes that are needed during runtime. That includes the GameWindow$2$10 classes, they are the anonymous inner classes that you wrote in your GameWindow class.
Other .jar files are not included in your .jar file, but you can reference them using the Class-path attribute in your manifest.
regarding your external dependencies, this will NOT work unless you compile in the dependencies as .class files, or use something like FatJar http://fjep.sourceforge.net/ to add them into one big jar.
You can create a .jar file, zipping the folder with the compiled classes and renaming the file.
For instance, if your class files are located in the "game" folder, zip it to game.zip, and rename it to game.jar
This is really simple and it works!

Categories