I am developing intellij plugin, on click of menu option it should copy jar to libs folder of select project. kindly provide any link or code how I can implement this.
jar is on this path "project_name\resources\raw\xyz.jar" and using below code to get that jar as inputsream but getting "NULL"
ClassLoader CLDR = this.getClass().getClassLoader();
InputStream is = CLDR.getResourceAsStream("raw/xyz.jar");
Try something like this:
PluginId runtimePluginId = PluginManager.getPluginByClassName("com.company.AnyClassInYourPlugin");
IdeaPluginDescriptor runtimePlugin = PluginManager.getPlugin(runtimePluginId);
File yourJar = new File(runtimePlugin.getPath().getAbsolutePath(), "your-file.jar");
Given any class that's contained in your plugin, the API allows you to retrieve a plugin descriptor for that plugin, which then allows you to get the absolute path to the plugin or any jar related to it.
See this real example of how to retrieve a JAR embedded in a plugin.
Related
I have java app, where I copy directories (with subdir and files) to SDCard. I wanted to save these dirs with project. In eclipse everything works fine. But, when I export project to runnable jar, app is not able to find or copy these dirs. No error is showed.
File folder2 = new File(getClass().getResource("/urgent/").getFile());
File folder4 = new File(getClass().getResource("/service/").getFile());
The path, I get from jar app, when I want to copy dir is:
jar:file:\C:\path\to\jar\file\myapp.jar!\urgent
project hierarchy:
-project
--src
--build
--urgent
---subdir
--service
---subdir
Is it possible to work with your own directories in jar and how?
You are trying to work on resources on you classpath, a random folder on your harddrive is not in it, so you cannot find it.
What you need to do is:
1. Work with parameters from the commandline (see how to use args[] in your main method)
2. use those arguments to parametrize your custom copy mehod
or:
Try using the copyDirectory(File srcDir, File destDir) method from the Apache Commons IO library instead. To use this reearch how to include libraries in your project, preferably by using maven.
I took the java implementation of the Factual API (reference http://developer.factual.com/) and made a JAR file for factual. I did this by opening a new project in eclipse with the factual java files and then exporting to a new jar file.
I put that jar file in my coldfusion installation's /WEB-INF/lib/ folder.
After restarting Coldfusion, I tried to create a new cfobject like so
<cfscript>
// Initialize the Java class.
factualClass=CreateObject("java", "src.main.java.com.factual.driver.Factual");
</cfscript>
I get an error indicating that it cannot find the Factual class.
Can anybody give me some guidance?
(Summary from comments)
It sounds like you may be exporting the source files ie *.java rather than the compiled class files, ie *.class. In the Jar Export wizard, be sure to select the "Export generated class files and resources" option. (To automatically compile the project sources before expi, enable the setting: JAR packaging > Build projects if not build automatically option). If you prefer you can also find pre-compiled jars in the MVN repository.
put that jar file in my coldfusion installation's /WEB-INF/lib/
folder.
CF10+ also supports dynamic class loading via a new application level setting THIS.javaSettings.
// Initialize the Java class.
factualClass=CreateObject("java", "src.main.java.com.factual.driver.Factual");
Just as a point of interest, src/main/java/ is not actually part of the libary class name. It is a standard directory structure used in Maven projects. It is probably included when exporting the sources, but not the compiled classes.
You can always verify the correct path and class name either by examining the API ie javadocs or by viewing one the source files. Package declarations are always at the top of the source file, such as on line 1 of src/main/java/com/factual/driver/Factual.java:
package com.factual.driver; // ie "com.factual.driver"
.. and the class declaration on line 39.
public class Factual { // ie "Factual"
Combined that gives you the exact (case-sensitive) path to use with createObject:
factualClass=CreateObject("java", "com.factual.driver.Factual");
I have some Java code that wraps an existing native application and performs the following:
Takes some input from the user
Executes a native application providing as parameters the input taken in step 1
Performs some more operations on the output files produced in step 2
The native application in step 2 requires some dynamic libraries. So, under Run Configurations -> Environment I have set the following variables to reference the libraries.
DYLD_LIBRARY_PATH = ${project_loc}/path/to/libs
DYLD_FALLBACK_LIBRARY_PATH = ${project_loc}/path/to/libs
And so far it all works. Now I have packaged my code and the existing native application as an Eclipse plugin. Whenever I try to run the code inside the plugin I get the following error:
dyld: Library not loaded: libsrcml.dylib
Referenced from: workspace/Project/src/nativeApp
Reason: image not found
To my understanding, this happens because the environment variables I had set previously reference {$project_loc}, which is the location where my Eclipse project was stored. Now, my code is no longer contained in that project, but it is contained inside a plugin, so the path for the variables no longer works. Question is, how can I set a path that references a folder inside my plugin? Alternatively, is it possible to, somehow, load those variables dynamically inside my Java code?
The path variables are used to specify a fixed location in the file system.
To identify a resource in a plugin, I would use its URL
Case 1: Platform.getBundle("").getEntry("")
Bundle bundle = Platform.getBundle("your.bundle.id");
URL url = bundle.getEntry("yourDir/yourFile.txt");
File f = new File(FileLocator.resolve(url).toURI());
Case 2 : Platform URL to your resource:
url = new URL("platform:/plugin/your.bundle.id/yourDir/yourFile.txt");
File f = new File(FileLocator.resolve(url).toURI());
Thanks to Vogella for this tip.
However, for libraries in your plug-in it is a little bit different, as System.loadLibrary("libname") must be able to resolve your lib.
If you ship and use native libraries in your plug-in, please package your plugin as a directory, and not as a compressed jar file.
So edit your plug-in's MANIFEST.MF and set your Eclipse-BundleShape: dir
Eclipse-BundleShape: dir
Then, your plug-in will be packaged as a folder, and then it is your responsibility to make your Native libraries interacting. Usually this depends on how the native libraries are linking each other, and on how your Java-to-native framework is setting the search paths.
My simple solution, is putting all the native libraries to the root folder of the Eclipse executable, which is the Java execution directory, so that I can get that path using the "user.dir" environment variable as follows:
System.getProperty("user.dir");
Then, when all the natives are in the same folder, they can reference each other without problems.
Please, also check these resources:
this StackOverflow answer
this eclipse forum answer
I am developing a plug-in for eclipse. The purpose of the plug-in is to generate code by using Acceleo.
I want to copy the contents of a directory from the generator plug-in jar, into the folder where files have been generated after the plug-in has finished generating the code.
org.plugin.generator/framework
||
copy contents to:
||
\/
Workspace/ProjectFolder/generatedFiles/classes
I can get the paths, but I can't figure out how to extract the framework folder from the jar and copy its contents in the current project folder.
Can anyone please provide some tips? I am a beginner at Eclipse plug-in development.
Also, this is my first question here, although I have been browsing for 1-2 years.
Here is a rough code snippet to read any file from a plugin and write it to a project. I do not know if there is a way to list files in a bundle folder, but you can always hardcode the list of files you need to copy.
Bundle bundle = Platform.getBundle( "your.plugin.id" );
InputStream stream = FileLocator.openStream( bundle, "path.in.plugin", false );
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject( "your.project" );
IFile file = project.getFile( "something/abc.txt" );
file.create( stream, true, null );
You will need to add exception handling and closing of the stream to the above snippet.
I have a Java project in Netbeans. It runs fine with Maven. So I assembled it. It contains the following code to load a file that is in the JAR:
ClassLoader loader = MyClass.class.getClassLoader();
SERVICE_URL = loader.getResource("my.wsdl");
This returns a URL like:
jar:file:/path/to/my/file/MyClass-1.0-SNAPSHOT-jar-with-dependencies.jar!/my.wsdl
but the library that needs this parameter doesn't appear to be able to use it.
Is there any way this file can be in the JAR and be referred to from the code like this?
You may have to use ClassLoader.getResourceAsStream(), copy it to a temporary file, and then create a URL with File.toURI().toURL()