I have a jar file, which I dynamically load during execution of my java file (say my.java).
I do it this way:
File newFile = new File("path");
JarFile newJar= new JarFile(newFile);
Now there is a particular java file inside this jar, which I know the name of (say known.class).
I want create an object of this known.class, and call a method inside it from my.java.
I am not sure how to go about this. Can any one help?
I tried looking online, but didn't find anything helpful.
Thanks.
You need a .class file (compiled java code) inside the jar, not a .java file. If you have this, add the .jar to your classpath, or to one of the directories in your classpath, and then you can simply reference the Object from your Java code.
Ie:
MyObject newObj = new MyObject();
Here's how your specify the jar in the classpath:
java -classpath ".;myjar.jar" org.mine.MyClass
Edit: Since you don't want to change the classpath, try something like this:
File file = new File("/path/to/myjar.jar");
URL url = file.toURL();
URL[] urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass("org.mine.myclass")
You need to create a URLClassLoader not a JarFile. Reading the doc for ClassLoader will get you going, and also for Class.
Related
This question already has an answer here:
How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?
(1 answer)
Closed 2 years ago.
My project is running properly in eclipse but when I am creating a jar file of this project and trying to run it through cmd it is showing "Location is not set" error.
My project structure is:
The Method is (Running in eclipse):
#FXML
private void RegularCustomer(ActionEvent event) throws Exception{
Stage stage = (Stage) dailySales.getScene().getWindow();
Scene scene = dailySales.getScene();
FXMLLoader loader = new FXMLLoader(getClass().getResource("../customer/CustomerHome.fxml"));
System.out.println(loader.getLocation());
scene.setRoot(loader.load());
stage.setScene(scene);
stage.show();
}
What is wrong with this code?
There are some relative questions but they are different from it. Their code didn't run in IDE but my code run in IDE.
FYI: I made some changes in folder structure and was able to run successfully. But that structure was horrible because I put all my FXML files and controllers in same package.
When you use getClass().getResource(...) you are loading a resource, not specifying a path to a file. In the case where the class loader loads classes from the file system, these essentially equate to the same thing, and it does actually work (though even then there's no technical reason it has to). When the class loader is loading classes by other mechanisms (and probably in all cases anyway), then it's important to pay attention to the Java specifications for a resource.
In particular, note:
Resources, names, and contexts
A resource is identified by a string consisting of a sequence of
substrings, delimited by slashes (/), followed by a resource name.
Each substring must be a valid Java identifier. The resource name is of the form shortName or shortName.extension. Both shortName
and extension must be Java identifiers.
(My emphasis.) Since .. is not a valid Java identifier, there's no guarantee of this resource being resolvable. It happens that the file system class loader resolves this in the way you expect, which is why it works in your IDE, but the implementation of getResource(...) in the jar class loader does not implement this in the way you are hoping.
Try
FXMLLoader loader = new FXMLLoader(getClass().getResource("/sm/customer/CustomerHome.fxml"));
Using controller locations to load FXML:
Since you have organized your code so that each FXML is in the same package as its corresponding controller file (which I think is a sensible way to do things), you could also leverage this in loading the FXML: just load the FXML "relative to its controller":
FXMLLoader loader = new FXMLLoader(CustomerHomeCtrl.class.getResource("CustomerHome.fxml"));
This seems fairly natural in this setup, and the compiler will check that you have the package name for CustomerHomeCtrl correct at the point where you import the class. It also makes it easy to refactor: for example suppose you wanted to split sm.admin into multiple subpackages. In Eclipse you would create the subpackages, drag and drop the FXML and controllers to the appropriate subpackages, and the import statements would automatically be updated: there would be no further changes needed. In the case where the path is specified in the getResource(...), all those would have to be changed by hand.
A bit late but this can maybe help someone. If you are using IntelliJ, your resources folder may not be marked as THE resources folder, which has the following icon:
This is the way I fixed it:
I am trying to make 3 folders in a Javafx application. I have a Views folder which will contain my views, and I want to load an fxml file saved inside Views. I wrote this code inside start method:
Parent root = FXMLLoader.load(getClass().getResource("/Views/ProductView.fxml"));
My folders are structured as follows:
Apparently GetResources() can't find my file. What am I doing wrong?
Problem is that loader cannot find fxml file ...
So, load method can be either empty or gets Inputstream argument.
And this should work:
FXMLLoader loader = new FXMLLoader();
FileInputStream fileInputStream = new FileInputStream(new File("src/main/java/CRUD/bnkseekCRUD.fxml"));
Parent root = loader.load(fileInputStream);
At least it works for me. )))
try something like this something like this
Parent root=FXMLLoader.load(getClass().getClassloader().getResource("application/Models/Views/ProductView.fxml")
I saved a PublicKey instance in a file using ObjectOutputStream. This file is then stored inside a jar file which is then loaded by JBoss. I'm trying to read this file but it throws me an exception telling that it's not serializable.
Here is the code :
InputStream input = KeyLoader.class.getClassLoader().getResourceAsStream(resource);
ObjectInputStream objectInputStream = new ObjectInputStream(input);
Object obj = objectInputStream.readObject();
Key output = (Key) obj;
objectInputStream.close();
return output;
which throws me this exception
An exception occurred: java.io.NotSerializableException
I'm not sure about serialization/deserialization from within a jar file, but
without being able to see the rest of your code, I can say a few things:
make sure that all of the classes that you are trying to serialize/deserialize implement Serializable.
If you can't do number 1, then you might try getting an encoded form of the Key object, such as key.getEcnoded(), which would allow you to do input/output using the bytes of the key.
Edit:
I'm not really familiar with JBoss, but maybe try using JBossObjectInputStream and JBossObjectOutputStream (org.jboss.serial.io). You will also have to add jboss-serialization.jar to your classpath. see link
also, I've never done this sort of thing before, but if you think the jar file is complicating things, but you should be able to use some of the classes in the java.util.jar package to simplify IO operations with jar files.
I hope this helps in some way.
i have an microsoft .office.interop.excel(dll) located at an directory d:\abc. now i do not want to add them as an web reference in my projet and call them
rather call the dll dynamically from my code behind(.cs)
is ther any way we can do dynmically
anyhelp would be great
thank you
Yes, but you will need to use reflection because if you don't add the assembly as reference it won't be known at compile time. Take a look at LoadFrom method.
var assembly = Assembly.LoadFrom(#"d:\abc\microsoft.office.interop.excel.dll");
var someType = assembly.GetType("Namespace.Type");
var instance = Activator.CreateInstance(type);
someType.InvokeMember(... // the reflection pain goes on
Take a look in Assembly.Load() method.
I want to discourage you from doing that. It can definitely be done if read the dll into a byte[] and call AppDomain.CurrentDomain.Load(byte[]). However you will find that you can only work with the types of that assembly through reflection. Otherwise your code behind file will not compile. So if at all possible you should add a reference (not a web reference) to the dll.
I know that it is possible to add swf metadata to the compile command as a command option, but I can't find any documentation on how to access these metadata within the actionscript during runtime. We're trying to add a version number to the swf during compile time and then somewhere in our app we would retrieve it during runtime, here is the command example to add the description metadata.
mxmlc -description "version 1.2.3"
I know the swf metadata is used by search engines and other utilities to gather information about the SWF file, but surely you should be able to retrieve them in the actionscript during runtime?
Have you tried to namespace it like this: http://hasseg.org/blog/?p=165
So maybe "-define+=VERSION::description,"version 1.2.3"
And then access it using the example code:
var VERSION:Namespace = new Namespace("VERSION");
var ver:String = VERSION::description;
EDIT: Hmm. Doesn't work for me in Flex Builder, but I found this: http://livedocs.adobe.com/flex/3/html/help.html?content=compilers_21.html, which seems to confirm the idea.