<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lorenzo Tomazzoni</title>
    <description>The latest articles on DEV Community by Lorenzo Tomazzoni (@lorenzo_tomazzoni_9fed461).</description>
    <link>https://dev.to/lorenzo_tomazzoni_9fed461</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2671827%2F428fe84a-6cfe-44bf-8847-ab346fd37248.png</url>
      <title>DEV Community: Lorenzo Tomazzoni</title>
      <link>https://dev.to/lorenzo_tomazzoni_9fed461</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lorenzo_tomazzoni_9fed461"/>
    <language>en</language>
    <item>
      <title>JS SERVER NOT WORKING WITH JARS</title>
      <dc:creator>Lorenzo Tomazzoni</dc:creator>
      <pubDate>Tue, 07 Jan 2025 19:44:13 +0000</pubDate>
      <link>https://dev.to/lorenzo_tomazzoni_9fed461/js-server-not-working-with-jars-27n1</link>
      <guid>https://dev.to/lorenzo_tomazzoni_9fed461/js-server-not-working-with-jars-27n1</guid>
      <description>&lt;p&gt;Question:&lt;br&gt;
I have two versions of a server using express.js. The first version should create a .zip file containing two .JAR files (Client.jar and Server.jar) but when I try to connect, it says "Cannot connect to the server".&lt;/p&gt;

&lt;p&gt;The second version simply creates a .zip with the .JAVA files, and it works perfectly.&lt;/p&gt;

&lt;p&gt;How can I modify the first version so that it works and successfully creates a .zip containing the .JAR files?&lt;/p&gt;

&lt;p&gt;First Version (Not Working - Attempts to generate .JAR files):&lt;br&gt;
javascript&lt;br&gt;
Copia codice&lt;br&gt;
const express = require('express');&lt;br&gt;
const cors = require('cors');&lt;br&gt;
const fs = require('fs');&lt;br&gt;
const path = require('path');&lt;br&gt;
const archiver = require('archiver');&lt;br&gt;
const { exec } = require('child_process');&lt;/p&gt;

&lt;p&gt;const app = express();&lt;br&gt;
app.use(cors());&lt;br&gt;
app.use(express.json());&lt;/p&gt;

&lt;p&gt;app.post('/generate-files', (req, res) =&amp;gt; {&lt;br&gt;
    const { ip, port } = req.body;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const clientFilePath = path.join(__dirname, 'Client.java');
const serverFilePath = path.join(__dirname, 'Server.java');

let clientFileContent = fs.readFileSync(clientFilePath, 'utf-8');
let serverFileContent = fs.readFileSync(serverFilePath, 'utf-8');

// Replace IP and port
clientFileContent = clientFileContent.replace('127.0.0.1', ip).replace('12345', port);
serverFileContent = serverFileContent.replace('12345', port);

const tempDir = path.join(__dirname, 'temp');
fs.mkdirSync(tempDir, { recursive: true });

// Write modified files to the temp directory
const tempClientPath = path.join(tempDir, 'Client.java');
const tempServerPath = path.join(tempDir, 'Server.java');
fs.writeFileSync(tempClientPath, clientFileContent);
fs.writeFileSync(tempServerPath, serverFileContent);

// Compile Java files
exec(`javac ${tempClientPath} ${tempServerPath}`, (err, stdout, stderr) =&amp;gt; {
    if (err) {
        console.error('Compilation Error:', stderr);
        res.status(500).send('Error compiling Java files.');
        return;
    }

    // Create JAR files
    const clientJarPath = path.join(tempDir, 'Client.jar');
    const serverJarPath = path.join(tempDir, 'Server.jar');

    exec(`jar cfe ${clientJarPath} Client -C ${tempDir} Client.class`, (err) =&amp;gt; {
        if (err) {
            console.error('Error creating Client.jar:', err);
            res.status(500).send('Error creating Client.jar.');
            return;
        }

        exec(`jar cfe ${serverJarPath} Server -C ${tempDir} Server.class`, (err) =&amp;gt; {
            if (err) {
                console.error('Error creating Server.jar:', err);
                res.status(500).send('Error creating Server.jar.');
                return;
            }

            // Create ZIP archive
            const output = fs.createWriteStream(path.join(__dirname, 'files.zip'));
            const archive = archiver('zip', { zlib: { level: 9 } });

            output.on('close', () =&amp;gt; {
                res.download(path.join(__dirname, 'files.zip'), 'files.zip', () =&amp;gt; {
                    fs.rmSync(tempDir, { recursive: true, force: true });
                    fs.unlinkSync(path.join(__dirname, 'files.zip'));
                });
            });

            archive.pipe(output);
            archive.file(clientJarPath, { name: 'Client.jar' });
            archive.file(serverJarPath, { name: 'Server.jar' });
            archive.finalize();
        });
    });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;// Start server on port 3000&lt;br&gt;
app.listen(3000, () =&amp;gt; {&lt;br&gt;
    console.log('Server running at &lt;a href="http://localhost:3000'" rel="noopener noreferrer"&gt;http://localhost:3000'&lt;/a&gt;);&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;Here is the formatted version of your question and code for Stack Overflow:&lt;/p&gt;

&lt;p&gt;Question:&lt;br&gt;
I have two versions of a server using express.js. The first version should create a .zip file containing two .JAR files (Client.jar and Server.jar) but when I try to connect, it says "Cannot connect to the server".&lt;/p&gt;

&lt;p&gt;The second version simply creates a .zip with the .JAVA files, and it works perfectly.&lt;/p&gt;

&lt;p&gt;How can I modify the first version so that it works and successfully creates a .zip containing the .JAR files?&lt;/p&gt;

&lt;p&gt;First Version (Not Working - Attempts to generate .JAR files):&lt;br&gt;
javascript&lt;br&gt;
Copia codice&lt;br&gt;
const express = require('express');&lt;br&gt;
const cors = require('cors');&lt;br&gt;
const fs = require('fs');&lt;br&gt;
const path = require('path');&lt;br&gt;
const archiver = require('archiver');&lt;br&gt;
const { exec } = require('child_process');&lt;/p&gt;

&lt;p&gt;const app = express();&lt;br&gt;
app.use(cors());&lt;br&gt;
app.use(express.json());&lt;/p&gt;

&lt;p&gt;app.post('/generate-files', (req, res) =&amp;gt; {&lt;br&gt;
    const { ip, port } = req.body;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const clientFilePath = path.join(__dirname, 'Client.java');
const serverFilePath = path.join(__dirname, 'Server.java');

let clientFileContent = fs.readFileSync(clientFilePath, 'utf-8');
let serverFileContent = fs.readFileSync(serverFilePath, 'utf-8');

// Replace IP and port
clientFileContent = clientFileContent.replace('127.0.0.1', ip).replace('12345', port);
serverFileContent = serverFileContent.replace('12345', port);

const tempDir = path.join(__dirname, 'temp');
fs.mkdirSync(tempDir, { recursive: true });

// Write modified files to the temp directory
const tempClientPath = path.join(tempDir, 'Client.java');
const tempServerPath = path.join(tempDir, 'Server.java');
fs.writeFileSync(tempClientPath, clientFileContent);
fs.writeFileSync(tempServerPath, serverFileContent);

// Compile Java files
exec(`javac ${tempClientPath} ${tempServerPath}`, (err, stdout, stderr) =&amp;gt; {
    if (err) {
        console.error('Compilation Error:', stderr);
        res.status(500).send('Error compiling Java files.');
        return;
    }

    // Create JAR files
    const clientJarPath = path.join(tempDir, 'Client.jar');
    const serverJarPath = path.join(tempDir, 'Server.jar');

    exec(`jar cfe ${clientJarPath} Client -C ${tempDir} Client.class`, (err) =&amp;gt; {
        if (err) {
            console.error('Error creating Client.jar:', err);
            res.status(500).send('Error creating Client.jar.');
            return;
        }

        exec(`jar cfe ${serverJarPath} Server -C ${tempDir} Server.class`, (err) =&amp;gt; {
            if (err) {
                console.error('Error creating Server.jar:', err);
                res.status(500).send('Error creating Server.jar.');
                return;
            }

            // Create ZIP archive
            const output = fs.createWriteStream(path.join(__dirname, 'files.zip'));
            const archive = archiver('zip', { zlib: { level: 9 } });

            output.on('close', () =&amp;gt; {
                res.download(path.join(__dirname, 'files.zip'), 'files.zip', () =&amp;gt; {
                    fs.rmSync(tempDir, { recursive: true, force: true });
                    fs.unlinkSync(path.join(__dirname, 'files.zip'));
                });
            });

            archive.pipe(output);
            archive.file(clientJarPath, { name: 'Client.jar' });
            archive.file(serverJarPath, { name: 'Server.jar' });
            archive.finalize();
        });
    });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;// Start server on port 3000&lt;br&gt;
app.listen(3000, () =&amp;gt; {&lt;br&gt;
    console.log('Server running at &lt;a href="http://localhost:3000'" rel="noopener noreferrer"&gt;http://localhost:3000'&lt;/a&gt;);&lt;br&gt;
});&lt;br&gt;
Second Version (Working - Generates .zip with .JAVA files):&lt;br&gt;
javascript&lt;br&gt;
Copia codice&lt;br&gt;
const express = require('express');&lt;br&gt;
const cors = require('cors');&lt;br&gt;
const fs = require('fs');&lt;br&gt;
const path = require('path');&lt;br&gt;
const archiver = require('archiver');&lt;/p&gt;

&lt;p&gt;const app = express();&lt;br&gt;
app.use(cors());&lt;br&gt;
app.use(express.json());&lt;/p&gt;

&lt;p&gt;app.post('/generate-files', (req, res) =&amp;gt; {&lt;br&gt;
    const { ip, port } = req.body;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const clientFilePath = path.join(__dirname, 'Client.java');
const serverFilePath = path.join(__dirname, 'Server.java');

let clientFileContent = fs.readFileSync(clientFilePath, 'utf-8');
let serverFileContent = fs.readFileSync(serverFilePath, 'utf-8');

// Replace IP and port
clientFileContent = clientFileContent.replace('127.0.0.1', ip).replace('12345', port);
serverFileContent = serverFileContent.replace('12345', port);

const tempDir = path.join(__dirname, 'temp');
fs.mkdirSync(tempDir, { recursive: true });

fs.writeFileSync(path.join(tempDir, 'Client.java'), clientFileContent);
fs.writeFileSync(path.join(tempDir, 'Server.java'), serverFileContent);

const output = fs.createWriteStream(path.join(__dirname, 'files.zip'));
const archive = archiver('zip', { zlib: { level: 9 } });

output.on('close', () =&amp;gt; {
    res.download(path.join(__dirname, 'files.zip'), 'files.zip', () =&amp;gt; {
        fs.rmSync(tempDir, { recursive: true, force: true });
        fs.unlinkSync(path.join(__dirname, 'files.zip'));
    });
});

archive.pipe(output);
archive.directory(tempDir, false);
archive.finalize();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;// Start server on port 3000&lt;br&gt;
app.listen(3000, () =&amp;gt; {&lt;br&gt;
    console.log('Server running at &lt;a href="http://localhost:3000'" rel="noopener noreferrer"&gt;http://localhost:3000'&lt;/a&gt;);&lt;br&gt;
});&lt;br&gt;
What I Need:&lt;br&gt;
I want the first version to work so that it can generate a .zip file containing the two .JAR files instead of the .JAVA files. How can I fix it?****&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
