I have written a program on 1 PC, and tested out very carefully, everything work fine.
But when I bring my .exe to another computer, it doesn’t
The problem I found is that it remembers the path of my first computer which is weird because I never hard-coded anything.
what this program does is firing a cmd that run java program using
Process.start();
this is the result:
javac: invalid flag: C:\Users\Lan
Usage: javac
use -help for a list of possible options
C:\Users\Lan Nguyen\Desktop\TestGrader\f1>
if you can see \TestGrader\f1; this is correct folder, it is where i want it to go. so the code works fine
but if you look at javac: invalid flag: C:\Users\Lan; this is the other computer where my code originally was made.
I do not know why this is the case, can someone help me fix it please.
Thank you.
btw, this has nothing to do with java, I just want to use c# to run a cmd that will later run java.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace Gui
{
public partial class GradingProgram : Form
{
private String[] studentName;
private int index = 0;
String projectName, cmd, compileJava, runJava, path, unzip;
public GradingProgram()
{
studentName = new String[50];
InitializeComponent();
cmd = "cmd";
unzip = "/K rar x ";
compileJava = "/K javac ";
button3.Enabled = false;
this.AutoSize = true;
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderChooser = new FolderBrowserDialog();
folderChooser.ShowDialog();
path = folderChooser.SelectedPath;
}
private void updateLabels()
{
textBox2.Clear();
for (int i = 0; i < index; i++)
{
textBox2.AppendText(studentName[i]);
textBox2.AppendText(Environment.NewLine);
}
}
private void button2_Click(object sender, EventArgs e)
{
String currentDirectory;
for (int i = 0; i < index; i++)
{
currentDirectory = @path + "\\" + studentName[i];
Directory.SetCurrentDirectory(currentDirectory);
foreach (String files in Directory.GetFiles(currentDirectory))
{
if (Path.GetExtension(files) == ".java")
{
Process.Start(cmd, compileJava + files).WaitForExit();
}
}
}
}
private void button3_Click(object sender, EventArgs e)
{
projectName = textBox1.Text;
runJava = "/K java " + projectName;
String currentDirectory;
for (int i = 0; i < index; i++)
{
currentDirectory = @path + "\\" + studentName[i];
Directory.SetCurrentDirectory(currentDirectory);
textBox3.ScrollBars = ScrollBars.Vertical;
foreach (String files in Directory.GetFiles(currentDirectory))
{
if (Path.GetExtension(files) == ".java")
textBox3.Text += File.ReadAllText(files);
textBox3.AppendText("------------------------------------------------------------");
}
Process.Start(cmd, runJava).WaitForExit();
try
{
textBox4.Text = File.ReadAllText(Directory.GetCurrentDirectory() + "\\" + "output.txt");
}
catch (Exception )
{
textBox4.Text = "no output";
}
}
}
private void button4_Click(object sender, EventArgs e)
{
Directory.SetCurrentDirectory(@path);
foreach (String files in Directory.GetFiles(path))
{
if (Path.GetExtension(files) == ".rar")
{
Process.Start(cmd, unzip + files).WaitForExit();
}
}
foreach (String folder in Directory.GetDirectories(path))
{
studentName[index] = new DirectoryInfo(folder).Name;
index++;
}
button4.Enabled = false;
}
private void button5_Click(object sender, EventArgs e)
{
updateLabels();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (this.Text != "")
{
button3.Enabled = true;
}
}
}
}
Shouldn’t you be using something like this:
Because it seems that it fails with the space in your username.
If you have accounts with the same name on both computers you also have two C:\Users\… directories.