This question already has answers here:
Java: Which of multiple resources on classpath JVM takes?
(3 answers)
Closed 1 year ago.
I am facing some difficulties with this package concept of Java. Let's assume I have the following two directory structures.
dir1
|___com
|___example
|___mypackage
|___myclass.class
dir2
|___com
|___example
|___mypackage
|___myclass.class
So both the myclass.class files are part of com.example.mypackage. Now if both dir1 and dir2 are on CLASSPATH and I write import com.exaple.mypackage.myclass, which path will be considered?
So, It is the responsibility of class loader to load classes. So first of all custom classes have been looked up by the JVM then it goes jar files to check the classes.
In your case it looks like both are custom classes hence to resolve this issue you have specify which file you want to load then you can add dir1 / dir2 to specify the exact class.
JVM picks the latest classes you entered in hierarchy.
Related
This question already has answers here:
Why do my java files look strange in Eclipse?
(5 answers)
Closed 1 year ago.
I have two class .java but in pdcController.java I cannot do anything:
Look the image:
Look the icon are different in BandoService works all, but in .pdfController I cannot do anything (for example I cannot suggest for method or error import). pdfController and BandoServiceare in the same project.
This is not the source file, check the file path, you're probably not in the correct package.
I guess you have several modules in your maven project and you opened the file from the parent module.
If you see the file only in the parent module it's time to git fetch.
This question already has answers here:
How to add resources to classpath
(3 answers)
What is a classpath and how do I set it?
(10 answers)
Closed 5 years ago.
I'm on a new branch of a git project on which I've made my own config file called MY_NAME.conf which is located in
Users/myname/IdeasProject/projectName/src/main/resources/config.
However, I'm having some issues getting my branch to run. So, just to make sure things are smooth I set up a new environment and I've cloned into the master. This is all done at
Users/myproject
However, now when I try to run the project I get a message
java.io.IOException: resource not found on classpath:
config/MY_NAME.conf
Why is it looking for this file at all? And even if it is, why is it not finding MY_NAME.conf?
Consider setting the classpath through one of these recommended techniques. Without knowing the specifics of your runtime environment, there should be a mechanism provided to include src/main/resources in your java classpath.
I think the issue is that classpath doesnt include conf folder.
You can modify/set class path with: set CLASSPATH=path1;path2
This question already has answers here:
How to use Java packages? [duplicate]
(3 answers)
Closed 7 years ago.
I am trying to create a java file that will read in xml files but for me to do this i need to use a DOM Parser which involves creating a package and declaring it and i was just wondering how do you do that
Packages in Java classes reflect the folder structure of your project. For example, if your project is located in the project folder, and the structure of your project is:
project\
project\mypackage\
project\mypackage\MyClass.java
then the MyClass.java file should contain the corresponding package declaration:
package mypackage;
public class MyClass {
You should also read a tutorial on packages to better understand how they work: https://docs.oracle.com/javase/tutorial/java/package/index.html
A package is declared as the first non-comment line in your Java source file:
package an.example.pkg;
public class Foo {
// code here
}
Will declare a class named Foo in a package called an.example.pkg.
Of course, the package is associated with a folder on your file system, so your .java file would actually need to be located in a directory called <some directory>/an/example/pkg in your file system (where <some directory> is a directory that is in your Java classpath).
The package tells Java where to search to find your classes, i.e. in which folders relative to your classpath to look.
This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 4 years ago.
I am compiling a program with multiple jar files (inside the lib folder) and classes (inside the src/com folder) with:
javac -classpath lib/\* src/com/*.java
I typed this to run the program:
java -cp lib/\* src/com/okc
But it doesn't work. Instead, I get this:
Error: Could not find or load main class src.com.okc
okc.java is the class containing the main method. How can I run a java program with multiple jar files and classes?
A Java class file is not just the file itself. The directory structure which represents a class's package is part of the class file. Your classpath needs to point to the directory which is the parent of the topmost package directory.
Assuming your class is declared with package com;, the topmost package directory is com. So you need the parent of com in your classpath:
java -classpath src:lib/\* com.okc
If your class does not contain any package statement, and you just happened to put it in a com directory, then it belongs to the null package, whose parent directory is com itself:
java -classpath src/com:lib/\* okc
An additional note: It is Java convention to have class names, and their respective file names, start with an uppercase letter. One reason is that it makes class names easy to distinguish from package components.
Try:
java -cp ../lib/\* com.okc
from the src directory (not sure...)
Assuming that your current directory has your lib/ :
java -cp lib src.com.okc
This question already has answers here:
Java resource as File
(6 answers)
Closed 9 years ago.
I have a jar file, say archive1.jar, resides in /home/ext/libs. This has a couple of classes, and also a text file (data.txt) at the root of the hierarchy of the jar. One of the classes of this jar (say Data.java) reads the file to initiate some internal data structure.
Now, the archive1.jar is included as a library of another program, called Parsing, which is executed in a completely different directory /home/progs (archive1.jar is specified in the classpath of the execution command of the program).
The problem I am facing is that now Data.java cannot understand the path of data.txt anymore, because it assumes that data.txt resides in /home/progs instead of being inside archive1.jar in /home/ext/libs.
My question is what change should I make inside Data.java in order for it to understand that the data.txt is inside the archive1.jar? In short, how to make Data.java be able to read data.txt inside archive1.jar, no matter where the calling program is.
Note: In thread Java resource as file, thing was a bit different, and also there was no answer to how to read the file inside the jar. One said it might be impossible.
The way to access resource files inside JAR archives is by using getClass().getResourceAsStream() . Using this ensures that the resource is loaded from the archive not from the filesystem. For example if you have data.txt in the same location as Data.java within the JAR archive, then you would access the data.txt in this way
InputStream is=getClass().getResourceAsStream("data.txt");
Once you have got an InputStream to the resource proceed with the way you like.