I would like to write java program that will print Class file name that Refer class inside JAR like below,
My jar file name : ExportExcel.jar it has class com/example/ExportXLSXExcel.class
below is my Java class that refer above class
import com.example.*;
Class DisputeProcess {
ExportXLSXExcel excel = new ExportXLSXExcel();
excel.download();
}
When I provide jar file name ExportExcel.jar. Java program should print DisputeProcess. Because DisputeProcess referred a class that inside ExportExcel.jar
Related
I was trying to compile a few files in a package in java. The package name is library. Please have a look at the following details.
This is my Directory Structure:
javalearning
---library
------ParentClass.java
------ChildClass.java
I tried to compile in the following way:
current directory: javalearning
javac library/ParentClass.java //this compilation works fine
javac library/ChildClass.java //error over here
The following is the ParentClass.java:
package library;
class Parentclass{
...
}
The following is the ChildClass.java:
package library;
class ChildClass extends ParentClass{
...
}
The error is as follows:
cannot access ParentClass
bad class file: .\library\ParentClass.class
Please remove or make sure it appears in the correct sub directory of the classpath
You've got a casing issue:
class Parentclass
That's not the same as the filename ParentClass.class, nor is it the same as the class you're trying to use in ChildClass: class ChildClass extends ParentClass.
Java classnames are case-sensitive, but Windows filenames aren't. If the class had been public, the compiler would have validated that the names matched - but for non-public classes, there's no requirement for that.
The fact that you've ended up with ParentClass.class suggests that at some point it was declared as ParentClass, but then you changed the declared name and when recompiling, Windows just overwrote the content of the current file rather than effectively creating Parentclass.class.
Make sure your declared class name exactly matches the filename. You may well want to delete all your class files before recompiling, just to get out of a confusing state.
I started to learn Java couple days ago. And I have this burning question. Is empty .java file name a valid source file name?
.java
Yes, save your java file by .java then compile it by javac .java and run by java yourclassname
class X {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
to compile - javac .java
to execute - java X
Yes it's working because java compiler doesn't consider it saves file name or not except our class having public specified we can save any name or empty but when ever trying to execute we must use our class name because, jvm creates byte code ourclassname.class so we using
java className
Yes Empty .java file name works, but class must not be public, it means that it must be default.
If class is public then following error occour:
D:\Testjavac>javac .java
.java:1: error: class Empty is public, should be declared in a file named Empty.
java
public class Empty
^
1 error
Yes You can have .java file withought nay name . you have to compile it by javac .java(it compile successfuly) and run it by java clasnname.(so you must provide a class name)
Yes, but don't do this often.
You can't create any classes in that file that are public or private, so any class that made use of any class defined here would have to be in the same package.
at anytime you can have only one public class in the file and if you use public class then that class name should be the file name.
I'd like to be able to load a class(es) from a known directory whenever a compiled .class file appears in that particular directory. However the I'd like the .class to be loaded regardless of what the package decleration is in its .java file. For example I have this class which I wish to load:
package com.javaloading.test;
public class SomeClassInPackage {
private String name = "The name of this Class is SomeClass.";
public String getName(){
return name;
}
}
And it is in the package com.javaloading.test. I then want to load it using this class:
public class GetPackage {
public static void main(String[] args){
new GetPackage().loadMyClass();
}
public void loadMyClass(){
// Get the current class loader
ClassLoader cl = getClass().getClassLoader();
try {
Object o = cl.loadClass("SomeClassInPackage");
System.out.println("Class loaded!");
} catch (ClassNotFoundException ex){
System.out.println("Could not load class");
}
}
}
If I put the .class files of both the above Classes into the same directory and run GetPackage it results in the error
Exception in thread "main" java.lang.NoClassDefFoundError:
SomeClassInPackage (wrong name:
com/javaloading/test/SomeClassInPackage
I need to be able to load a class (from a file) regardless of it's declared package and without having to actually know its package. I would then examine the loaded class for its package information. Is this possible using the System ClassLoader or a custom ClassLoader or is it impossible without having knowledge of the package structure? If it's possible any advice is appreciated.
It is impossible to load the class without its respective package structure, means if you want to load the class then it must be placed in the folder that is correspond to its packages name or that class is in a jar file but in same folder structure.
But lets say you want to load the classes which is external means not in the class path where this program gets executed from and you want to load it in current class loader during execution. Refer to this link How to load the classes at runtime. This will also gives answer to your next question where you want to load the classes which is selected by the program based on its name or package.
How can one create a package in Java:
In a book i read its :
package package_name
public class whatever{}
.
.
.
But shouldn't this be enclosed in parenthesis such as :
package package_name
{
public class whatever{}
.
.
.
}
Just a minor confusion. Can anyone give me an example of the correct syntax?
The syntax for creating package
package package_name;
public class Whatever {
}
You can find more useful info Here.
When creating a package, you should choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package.
Reference
Example :
package illustration; <------------
import java.awt.*;
public class Drawing {
. . .
}
Package is created as follows
package package_Name;
This package name has to be the first statement in the file.Once you declare package name start defining methods or classes or interfaces in it.
If this package you have to use in your any java file then write
import package_Name;
so that all the methods defined in the package will be accessible in java file.
No parenthesies. Package is nothing else then just a folder or set of folders. For example, if you create package named: utils, new folder will be created in your source folder. If you create package named org.utils, two folders will be created in your source folder. org and utils which will be inside of org folder. Also, every next package which starts with org. (for example org.ui) will be just a new folder created in org folder. So your folder hierarchy will look like this:
org--
|
utils
|
ui
If there is more than one class in one Java source file then what will be the file name of the .java file?
there can only be one public top level class in a java file. The name of the public class must match that of the file name. Other than this, there can be as many non public (default/package access) classes as you like.
None of this is part of any java specification, it is just convention but a very convenient one. This 'convention' also includes such things as java and class files being found in directory structures matching the package name of the class. Check out the java tutorial on this.
Only one class can be public in the same file and the public class is the file name...