How to validate if a file is executable in Java

Our goal today is to check the validity of a file by testing if it can be executed. Nothing to worry about. We’ve got an API for that.

Installing our library will get our ball rolling. All that this requires is adding two reference snippets to the Maven POM file.





Now that our library is installed, we can move right along to calling our function, validateDocumentExecutableValidation, and providing a file to validate.

// Import classes://import com.cloudmersive.client.invoker.ApiClient;//import com.cloudmersive.client.invoker.ApiException;//import com.cloudmersive.client.invoker.Configuration;//import com.cloudmersive.client.invoker.auth.*;//import com.cloudmersive.client.ValidateDocumentApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");Apikey.setApiKey("YOUR API KEY");// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.setApiKeyPrefix("Token");ValidateDocumentApi apiInstance = new ValidateDocumentApi();File inputFile = new File("/path/to/file"); // File | Input file to perform the operation on.try {DocumentValidationResult result = apiInstance.validateDocumentExecutableValidation(inputFile);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling ValidateDocumentApi#validateDocumentExecutableValidation");e.printStackTrace();}

Our file will now be tested and its validity will be returned in this format:

"DocumentIsValid": true,
"ErrorCount": 0,
"WarningCount": 0,
"ErrorsAndWarnings": [
"Description": "string",
"Path": "string",
"Uri": "string",
"IsError": true
Image for post

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store