Highlighted
cwBrendan Absent Member.
Absent Member.
2213 views

Executing Java code on remote machine (Silk4J)

My team is currently putting together tests that use two machines. We have been able to use two Desktop objects to drive tests on two machines but our issue is with running Java code that doesn't use the Desktop object. For example, we use Java's Files object to copy test files, check for file existence etc. These are all part of Java so they only run in the local Java virtual machine. We are looking for a way to run these methods in a way that can access the 2nd machine.

This is really a Java question but I figure we can't be the only people who have run into this issue with Silk4J.

Tags (1)
0 Likes
4 Replies
Micro Focus Expert
Micro Focus Expert

RE: Executing Java code on remote machine (Silk4J)

Hi,

There is no way for your Java code to execute on a remote system unless it is executed on that system directly. If you need to copy files on the remote system, it may be best to share the drive/folders required so that you can perform the required file operations remotely.

-Robert

0 Likes
cwBrendan Absent Member.
Absent Member.

RE: Executing Java code on remote machine (Silk4J)

I was worried that would be the answer.

Thankfully we've figured out that by using desktop.createAgentDllCall() we can create our own methods that access Windows DLLs to perform our desired actions on the remote machines.

0 Likes
abdulH1 Absent Member.
Absent Member.

RE: Executing Java code on remote machine (Silk4J)

Can you please share a snippet of code how you achieved it?

0 Likes
cwBrendan Absent Member.
Absent Member.

RE: Executing Java code on remote machine (Silk4J)

Here is an example that copies and replaces a file. It references IKernel32DllFunctions which is a simple class that exposes methods in Kernel32.dll. You should be able to figure out what goes in that class via the help. Please excuse the formatting, I can't figure out how to form this like code.

/**
* Copies the specified source file to the target file or folder path, replacing the target file if it already exists.
*
* To indicate that the targetFileOrFolderPath parameter is a folder to be created, the targetFileOrFolderPath should end with a backslash. If the target is a folder that does not exist, the folder will be created.
*
* If the target is a file that already exists, the file will be replaced.
*
* If the target is a folder that already exists, this method will copy the file into the folder, using the name of the source file.
*
* To copy multiple files into a folder, use {@code copyAndReplaceFiles}.
*
* The method calls the CopyFileW method in kernel32.dll using the agent.
*
* @param sourceFilePath The path to the source file
* @param targetFileOrFolderPath The path to the target file/folder destination
*/
public void copyAndReplaceFile(final String sourceFilePath, String targetFileOrFolderPath) {
if (!this.exists(sourceFilePath)) {
throw new RuntimeException("Could not copy and replace '" + sourceFilePath + "' to '" + targetFileOrFolderPath + "'. Source file does not exist.");
}
if (this.isDirectory(sourceFilePath)) {
throw new RuntimeException("Could not copy and replace '" + sourceFilePath + "' to '" + targetFileOrFolderPath + "'. Source file is a directory.");
}

targetFileOrFolderPath = this.createTargetFilePathString(sourceFilePath, targetFileOrFolderPath);

IKernel32DllFunctions kernel32Function = DllCall.createAgentDllCall(IKernel32DllFunctions.class, this.getDesktop());

int iSuccess;
iSuccess = kernel32Function.CopyFileW(sourceFilePath, targetFileOrFolderPath, false);

if (iSuccess == 0) {
throw new RuntimeException("Could not copy and replace '" + sourceFilePath + "' to '" + targetFileOrFolderPath + "'. DLL call did not return success.");
}

Logger.getGlobal().fine("Copied '" + sourceFilePath + "' to '" + targetFileOrFolderPath + "'.");
}

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.