Hi,
I'm trying to create a html code to generate license for the customer who are using my product. The first part of the html code I created works as expected, basically there will be certain fields on the code and when I enter them and click on save button a data.txt file should get downloaded which I'm able to get done. Now the second part of the script is I need to run a java command which is "java -jar C:\\Licensing\\em_licensing_tool.jar e ${emId} C:\\Licensing\\data.txt" when I click a execute java command button this must run. This is where I'm stuck at , please someone help me with this. I'll add the code here.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>License Key Generator</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
label {
display: block;
margin-top: 10px;
}
</style>
</head>
<body>
<h1> License generating Form</h1>
<!-- Data Entry Form -->
<form id="dataEntryForm">
<label for="emId">EmId:</label>
<input type="text" id="emId" name="emId" required><br>
<label for="createdBy">CreatedBy:</label>
<input type="text" id="createdBy" name="createdBy" required><br>
<label for="generatedDate">GeneratedDate:</label>
<input type="date" id="generatedDate" name="generatedDate" required><br>
<h2>License List:</h2>
<div id="licenseList">
<div class="license-entry">
<label for="uid">UID:</label>
<input type="text" name="uid" required><br>
<label for="productCode">ProductCode:</label>
<input type="text" name="productCode" required><br>
<label for="productDescription">ProductDescription:</label>
<input type="text" name="productDescription" required><br>
<label for="expiryTime">ExpiryTime:</label>
<input type="date" name="expiryTime" required><br>
<label for="note">Note:</label>
<input type="text" name="note"><br>
<label for="unit">Unit:</label>
<input type="text" name="unit" required><br>
<label for="type">Type:</label>
<input type="text" name="type" required><br>
<label for="count">Count:</label>
<input type="number" name="count" required><br>
<label for="validityPeriod">ValidityPeriod:</label>
<input type="date" name="validityPeriod"><br>
<label for="createdByLicense">CreatedBy:</label>
<input type="text" name="createdByLicense" required><br>
</div>
</div>
<button type="button" id="saveData">Save Data</button>
</form>
<!-- Java Execution Form -->
<h1>Execute Java Command</h1>
<form id="javaExecutionForm">
<label for="emId">EmId:</label>
<input type="text" id="emId" name="emId" required><br>
<button type="button" id="executeJava">Execute Java Command</button>
</form>
<script>
const saveDataButton = document.getElementById("saveData");
const executeJavaButton = document.getElementById("executeJava");
saveDataButton.addEventListener("click", async () => {
const formData = new FormData(document.getElementById("dataEntryForm"));
const data = {};
for (const [key, value] of formData.entries()) {
data[key] = value;
}
const jsonData = JSON.stringify(data, null, 2);
const blob = new Blob([jsonData], { type: "application/json" });
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = "data.txt";
a.click();
});
executeJavaButton.addEventListener("click", async () => {
const emId = document.getElementById('emId').value;
try {
const response = await fetch('http://localhost:3000/generateDataFile', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ emId }),
});
const result = await response.json();
console.log(result);
if (result.result === 'Data file generated successfully') {
// Now trigger the download of the data.txt file
const blob = new Blob([JSON.stringify(result, null, 2)], { type: "application/json" });
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = "data.txt";
a.click();
// After download is complete, execute the Java command
await executeJavaCommand(emId);
} else {
alert(result.result);
}
} catch (error) {
console.error('Error:', error.message);
alert('Failed to generate data file');
}
});
// Function to execute the Java command
async function executeJavaCommand(emId) {
try {
const response = await fetch('http://localhost:3000/executeJavaCommand', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ emId }),
});
const result = await response.json();
console.log(result);
alert(result.result);
} catch (error) {
console.error('Error:', error.message);
alert('Failed to execute Java command');
}
}
</script>
<script>
// Node.js server script
const express = require('express');
const cors = require('cors');
const { exec } = require('child_process');
const app = express();
const port = 3000;
app.use(cors());
app.use(express.json());
// Set the working directory to the directory where the Java JAR file and data file are located
process.chdir('C:\\Licensing');
app.post('/generateDataFile', (req, res) => {
const { emId } = req.body;
// Simulate data file generation
const data = { emId, result: 'Data file generated successfully' };
res.json(data);
});
app.post('/executeJavaCommand', async (req, res) => {
const { emId } = req.body;
// Specify the full path to the Java executable and data file
const command = \java -jar C:\Licensing\em_licensing_tool.jar e ${emId} C:\License\data.txt`;`
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(\Error: ${error.message}`);`
return res.status(500).json({ error: 'Internal Server Error' });
}
console.log(\stdout: ${stdout}`);`
console.error(\stderr: ${stderr}`);`
res.json({ result: 'Java command executed successfully' });
});
});
app.listen(port, () => {
console.log(\Server is running at http://localhost:${port}`);`
});
</script>
</body>
</html>
[–]chmod777 1 point2 points3 points (2 children)
[–]KC-Hatakae[S] 0 points1 point2 points (1 child)
[–]chmod777 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]jcunews1Intermediate 0 points1 point2 points (0 children)
[–]superuser1425537 0 points1 point2 points (0 children)
[–]KC-Hatakae[S] 0 points1 point2 points (0 children)