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

0

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.

0

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

For Downloading file

private void getLibraryToLocal(String downloadUrl, String libName) {

ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
@Override
public void run() {

try {

ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
indicator.setText("Downloading Jar");
indicator.setFraction(0.5);
indicator.setIndeterminate(true);

if (indicator.isCanceled()) {
throw new ProcessCanceledException();
}

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();
Project project = event.getRequiredData(CommonDataKeys.PROJECT);
String projectLocation = project.toString();
projectLocation = projectLocation.substring(projectLocation.indexOf("'") + 1, projectLocation.lastIndexOf("'"));
System.out.println(projectLocation);
File file = new File(projectLocation + "/" + libName);
file.deleteOnExit();
System.out.println(file);
file.createNewFile();
// FileUtils.copyInputStreamToFile(is, file);
copyInputStreamToFile(is, file);

System.out.println(file.getPath());
}

} catch (Exception ex) {
ex.printStackTrace();
}

}
}, "Establishing Connection... ", true, event.getProject());


}

private static void copyInputStreamToFile(InputStream inputStream, File file)
throws IOException {
try (FileOutputStream outputStream = new FileOutputStream(file)) {
int read;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
}
}

After this step, I am getting jar file to root folder

Next I have to attach this jar to modulles -> dependencies tab in project structure
I have done this in 2 ways

Method 1: took reference from https://www.jetbrains.org/intellij/sdk/docs/reference_guide/project_model/library.html#creating-a-libraryTook
/**
*
* @param project
* @param libraryName
* @param libraryPath is rootLocation/libName (as downloaded from just above method)
*/
private void createLibraryDependency(Project project, String libraryName, String libraryPath) {
LibraryTable projectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(project);
final LibraryTable.ModifiableModel projectLibraryModel = projectLibraryTable.getModifiableModel();

final Library library = projectLibraryModel.createLibrary(libraryName);
final Library.ModifiableModel libraryModel = library.getModifiableModel();

VirtualFile file = VfsUtil.findFileByIoFile(new File(libraryPath), true);

final Module module = ModuleManager.getInstance(event.getProject()).getModules()[0];

if (file != null) {
System.out.println(file);
libraryModel.addRoot(file, OrderRootType.CLASSES);
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
libraryModel.commit();
projectLibraryModel.commit();
ModuleRootModificationUtil.addDependency(module, library);
}
});
}

}

Method 2:
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;

System.out.println(clzUrlString);
System.out.println(srcUrlString);
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, null);
System.out.println(newLib);
ModuleRootModificationUtil.addModuleLibrary(module, libName, new ArrayList<String>() {{
add(clzUrlString);
}}, new ArrayList<String>() {{
add(srcUrlString);
}});
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);
System.out.println(libName+" "+library);
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;
}

I have tried the two methods both are adding jar files to modules -> dependenciestab

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


0

Hi Jakub
Any update on the above scenario.
Please help me coming out from this issue
Thank you !!!

0

请先登录再写评论。