Not able to set the jar file to classpath
Hi team, I am developing a plugin where i have download jar libraries and then set to the classpath. I am able to get the file to project and also jars are getting added to project structure -> modules-> dependencies
But still i am not able to use the java classes or packages which are available in the library. I am sharing the logic of my code pls try to help me.
getLibraryToLocal Methods will download the jar to local
private void getLibraryToLocal1(String downloadUrl, String libName) {
try{
URL obj = new URL(downloadUrl);
HttpURLConnection httpConnection = (HttpURLConnection) obj.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setRequestProperty("Content-type", "application/json");
httpConnection.setUseCaches(false);
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);
if(httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = httpConnection.getInputStream();
byte[] buffer = new byte[is.available()];
is.read(buffer);
Project project = event.getRequiredData(CommonDataKeys.PROJECT);
String projectLocation = project.toString();
projectLocation = projectLocation.substring(projectLocation.indexOf("'")+1, projectLocation.lastIndexOf("'"));
File file = new File(projectLocation+"/"+libName);
file.deleteOnExit();
file.createNewFile();
System.out.println(file);
Files.write(buffer, file);
// VirtualFileManager.getInstance().syncRefresh();
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
Attach Library method will attach libraries to project structure
private void attachJarLibrary(@NotNull final Module module,
@NotNull final String libName,
@NotNull final String jarFilePath,
@NotNull final String srcFilePath)
{
ApplicationManager.getApplication().runWriteAction(() ->
{
final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
final String clzUrlString = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, jarFilePath) + JarFileSystem.JAR_SEPARATOR;
final String srcUrlString = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, srcFilePath) + JarFileSystem.JAR_SEPARATOR;
final VirtualFile jarVirtualFile = VirtualFileManager.getInstance().findFileByUrl(clzUrlString);
final VirtualFile srcVirtualFile = VirtualFileManager.getInstance().findFileByUrl(srcUrlString);
final ModifiableRootModel modifiableModel = rootManager.getModifiableModel();
final Library newLib = createJarLib(libName, modifiableModel.getModuleLibraryTable(), jarVirtualFile, srcVirtualFile);
System.out.println(newLib);
modifiableModel.commit();
});
}
private Library createJarLib(@NotNull final String libName,
@NotNull final LibraryTable table,
@Nullable final VirtualFile jarVirtualFile,
@Nullable final VirtualFile srcVirtualFile)
{
Library library = table.getLibraryByName(libName);
if (library == null)
{
library = table.createLibrary(libName);
Library.ModifiableModel libraryModel = library.getModifiableModel();
if (jarVirtualFile != null)
{
libraryModel.addRoot(jarVirtualFile, OrderRootType.CLASSES);
}
if (srcVirtualFile != null)
{
libraryModel.addRoot(srcVirtualFile, OrderRootType.SOURCES);
}
libraryModel.commit();
}
return library;
}
Please let me know is this the correct way to do or is there any other way to add this library into the project
请先登录再写评论。
Ksnh Hnsk,
Sorry for the late reply. Can I ask you to provide a repository with a minimal reproducible example so I'll be able to debug it locally? Thanks.
Hi Jakub,
Thanks for the response. Sorry, I cannot send a repository by any luck.
I am sharing the steps and code which i have done please try to verify
I have created a project as new->Project-> Intellij Platform Plugin
Then I have created a class where first i am getting the jar to local(project root folder) later i am attaching the jar to file->project structure -> modules -
> dependencies tab
But none of the jar classes are reflecting into the project
Example:
I have downloaded opencsv 5.2 to local root folder
then in file -> project structure -> modules -> dependencies tab -> i am able to see opencsv 5.2
But In the java class if i am trying to acces com.opencsv classes i am not getting whereas if i am remove the jar from the depencies tab and manually if i add the same jar then i am able to see all the classes available in com.opencsv
I am not understanding why this behaviour is happening
Please try to verify the code and help me
Hi Jakub
Any update on the above scenario.
Please help me coming out from this issue
Thank you !!!