Loading aspose license in aspose java

I am trying out Aspose PDF for Java and I have encountered a problem. It seems that I need to load the license file every time I try to read a PDF file. i.e call setLicense in every class.

Is there any optimal way like load lic in static block across all classes in the application ?

Hi Mrinmoy,


Thanks for your inquiry. Please note that the license only needs to be set once per application/process and you need to set the license before using any other Aspose.Pdf for Java object it will work for all classes of Aspose.Pdf for Java in that session.

Although setting license multiple times is not harmful, but simply wastes processor time. So if you are developing a class library, you can call License.setLicense from a static constructor of your class that uses Aspose.Pdf. The static constructor will execute before an instance of your class is created making sure Aspose.Pdf license is properly set.

Please feel free to contact us for any further assistance.

Best Regards,

Hi Tilal,

Thanks for your reply. But I am still not sure how to achieve this. First of all I didn’t understand what do you mean by “the license only needs to be set once per application/process”. What is application/process ? Are you referring to current thread ? Till now what I have manged to do is to initiate the license in a static block (as you said too) in a class and the functions of the class works very well.
But I need to initialize the license in Evey class that use Aspose.pdf, which is a not so good as I have to keep informing every developer to do so (I can create an abstract class but then every one need to extend that which is a similar problem)

I have used Aspose Cells before. What I used to do is to initiate the license in Application start-up (spring context initialization / or in any class that gets created during spring start-up) in a static block and it was effective for every class that use aspose cells. But now with Aspose PDF it’s not the same.
Note : I am not creating a class library

Hi Mrinmoy,

Thanks for your feedback.

mrinmoy_chakraborty:

Thanks for your reply. But I am still not sure how to achieve this. First of all I didn't understand what do you mean by "the license only needs to be set once per application/process". What is application/process ? Are you referring to current thread ? Till now what I have manged to do is to initiate the license in a static block (as you said too) in a class and the functions of the class works very well.
But I need to initialize the license in Every class that use Aspose.pdf, which is a not so good as I have to keep informing every developer to do so (I can create an abstract class but then every one need to extend that which is a similar problem)

Application mean set of programs to achieve you specific goals/requirements. As you are using Aspose.Pdf for Java in your code so by "the license only needs to be set once per application/process" mean set license of Aspose.Pdf for Java once in start of your program before using any object of Aspose.Pdf for Java.

mrinmoy_chakraborty:

I have used Aspose Cells before. What I used to do is to initiate the license in Application start-up (spring context initialization / or in any class that gets created during spring start-up) in a static block and it was effective for every class that use aspose cells. But now with Aspose PDF it's not the same.
Note : I am not creating a class library

If you are not creating some class library but web application then may apply the license in a start up servlet init() method using stream.

public void init(ServletConfig config) throws ServletException {

super.init(config);

javax.servlet.ServletContext context = getServletContext();

License license = new License();

// getting license stream

InputStream resourceLic = context.getResourceAsStream("/Aspose.Pdf.Java.lic");

}


Please feel free to contact us for any further assistance.

Best Regards,