For this application, we need many files where we can store the data. These files are stored in the "Files" folder at application location.
appletcode.slvjapplet file
config.slvj file
defaultprojloc.slvj file
files.slvj file
jkeywords.slvj file
jpackages.slvj file
mainhtmlsource.slvj main HTML file
themesfile.slvj theme file
<project_name>.slvjproj
Download the source code.
appletcode.slvjapplet
This file contains HTML source code with <applet></applet> tag.
config.slvj
This file contains the data about everything in our application like JDK path or browser path to show ToolStrip, StatusStrip, or not, SplitContainer values. e.g:
- <?xml version="1.0" encoding="utf-8"?>
- <!--
- Silver-J
- Free light weight IDE for Java
- Copyright(c) 2016. All rights reserved
- -->
- <SilverJConfiguration>
-
- <JDKPath>C:\Program Files\Java\jdk1.8.0_25\bin</JDKPath>
-
- <WebBrowser>C:\Program Files (x86)\Google\Chrome\Application\chrome.exe</WebBrowser>
-
- <Font>Microsoft YaHei UI</Font>
- <FontSize>10</FontSize>
-
-
- <Appearance>Default</Appearance>
-
- <ShowLineNumbers>true</ShowLineNumbers>
- <ShowLineHighlighter>true</ShowLineHighlighter>
- <ShowInvalidLines>false</ShowInvalidLines>
- <ShowEndOfLineMarker>false</ShowEndOfLineMarker>
- <ShowVisibleSpaces>false</ShowVisibleSpaces>
- <BracesMatching>true</BracesMatching>
- <AutoCompleteBraces>true</AutoCompleteBraces>
-
- <SplitContainer1>1132</SplitContainer1>
- <SplitContainer2>559</SplitContainer2>
- <SplitContainer3>280</SplitContainer3>
- <SplitContainer4>315</SplitContainer4>
-
- <TabsAlignment>Top</TabsAlignment>
- <ShowStatusStrip>true</ShowStatusStrip>
- <ShowToolStrip>true</ShowToolStrip>
- <ShowProjectExplorer>true</ShowProjectExplorer>
- <ShowClassesView>true</ShowClassesView>
- <ShowMethodsView>true</ShowMethodsView>
- <ShowErrorList>true</ShowErrorList>
- <ShowErrorDialog>false</ShowErrorDialog>
- <ShowStartPageOnStartUp>true</ShowStartPageOnStartUp>
-
- <AutoCompileJava>false</AutoCompileJava>
-
- <AutoCompletion>true</AutoCompletion>
- </SilverJConfiguration>
defaultprojloc.slvj file
This file contains the current created or opened project name, project folder path, project folder file (.slvjproj) and project type.
- <?xml version="1.0" encoding="utf-8"?>
- <SilverJ>
- <DefaultProjectLocation>C:\My Java Projects</DefaultProjectLocation>
- <CurrentProjectName>LookAndFeelDemo</CurrentProjectName>
- <CurrentProjectFileName>C:\My Java Projects\LookAndFeelDemo\LookAndFeelDemo.slvjproj</CurrentProjectFileName>
- <CurrentProjectType>ApplicationType</CurrentProjectType>
- </SilverJ>
files.slvj file
This file contains the file names that are read from <VisualFile></VisualFile> tag from opened project file name.
jkeywords.slvj file
This file contains the Java keywords which will be used for code completion (see MyTabPage.cs file).
jpackages.slvj file
This file contains the Java packages which will be used to insert packages option Edit->Insert->Packages.
mainhtmlsource.slvj main html file
Contains the HTML source code when New->HTML file is added or created.
themesfile.slvjtheme file
This file contains the Java code when we create Java Class with Java Themes.
<project_name>.slvjproj
This file is stored in the users project folder. This file contains ProjectName, ProjectLocationFolder, ProjectType, MainClassFile, JavaClassFile, VisualFile, OtherFile. e.g of LookAndFeelDemo.slvjproj
- <?xml version="1.0" encoding="utf-8"?>
- <SilverJProject>
-
- <ProjectName>LookAndFeelDemo</ProjectName>
- <ProjectLocationFolder>C:\My Java Projects\LookAndFeelDemo</ProjectLocationFolder>
- <ProjectLocationFile>C:\My Java Projects\LookAndFeelDemo\LookAndFeelDemo.slvjproj</ProjectLocationFile>
- <ProjectType>ApplicationType</ProjectType>
- <MainClassFile>C:\My Java Projects\LookAndFeelDemo\srcclasses\LookAndFeelDemo.java</MainClassFile>
- <JavaClassFile>C:\My Java Projects\LookAndFeelDemo\srcclasses\JavaBlueTheme.java</JavaClassFile>
- <JavaClassFile>C:\My Java Projects\LookAndFeelDemo\srcclasses\JavaGreenTheme.java</JavaClassFile>
- <JavaClassFile>C:\My Java Projects\LookAndFeelDemo\srcclasses\JavaRedTheme.java</JavaClassFile>
- <JavaClassFile>C:\My Java Projects\LookAndFeelDemo\srcclasses\LookAndFeelDemo.java</JavaClassFile>
- <JavaClassFile>C:\My Java Projects\LookAndFeelDemo\srcclasses\TestFrame.java</JavaClassFile>
- <VisualFile>C:\My Java Projects\LookAndFeelDemo\srcclasses\LookAndFeelDemo.java</VisualFile>
- <VisualFile>C:\My Java Projects\LookAndFeelDemo\srcclasses\JavaGreenTheme.java</VisualFile>
- <VisualFile>C:\My Java Projects\LookAndFeelDemo\srcclasses\JavaRedTheme.java</VisualFile>
- <VisualFile>C:\My Java Projects\LookAndFeelDemo\srcclasses\TestFrame.java</VisualFile>
- </SilverJProject>
Visual file is the Java Class file which is only used to open this file in tab or not, when the project is opened.
In the file New_JavaApplicationProject_Form.cs, we will just read a file \\files\\defaultprojloc.slvjfile .
We will also take input from the above form as Project name, project location etc.
I have used the following variable to store it.
- String projloc = ProjectLocationTextBox.Text;
- String projfolder = projectfolderlabel.Text;
- String projectname = ProjectNameTextBox.Text;
- String projectfile;
Now, we need to create a folder (directory) same as project name in the read project location folder (projfolder).Once the directory is created, we will create classes, src, srcclasses directories in it.
Once the project directory is created, we need to create project file with file extension as .slvjproj (file name is same as created project name). In that file, we will save our project data as defined in above File section. We will save the data in XML format.
Following is the function that performs actions.
- public void CreateJavaProject()
- {
- String projloc = ProjectLocationTextBox.Text;
- String projfolder = projectfolderlabel.Text;
- String projectname = ProjectNameTextBox.Text;
- String projectfile;
-
- if (projloc != "" && projectname != "")
- {
- projectfile = projectname + ".slvjproj";
- if (checkBox1.Checked == false)
- {
- if (Directory.Exists(projloc + "\\" + projfolder))
- {
- MessageBox.Show("Entered project name folder is already exists in current location","Error............");
- }
- else
- {
-
- Directory.CreateDirectory(projloc + "\\" + projfolder);
-
-
- Directory.CreateDirectory(projloc + "\\" + projfolder + "\\classes");
-
-
- using (XmlWriter xmlwriter = XmlWriter.Create(projloc + "\\" + projfolder + "\\" + projectfile))
- {
- xmlwriter.WriteStartDocument();
- xmlwriter.WriteStartElement("SilverJProject");
- xmlwriter.WriteString("\n");
- xmlwriter.WriteComment("Silver-J (1.0) Java Application Project");
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("ProjectName", projectname);
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("ProjectLocationFolder", projloc + "\\" + projfolder);
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("ProjectLocationFile", projloc + "\\" + projfolder + "\\" + projectfile);
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("ProjectType", "ApplicationType");
- xmlwriter.WriteEndElement();
- xmlwriter.WriteEndDocument();
- xmlwriter.Close();
- }
-
- String defaultprojfilepath = Application.StartupPath + "\\files\\defaultprojloc.slvjfile";
-
- projectfilename = projloc + "\\" + projfolder + "\\" + projectfile;
-
- XmlDocument doc = new XmlDocument();
- doc.Load(defaultprojfilepath);
- doc.SelectSingleNode("SilverJ/DefaultProjectLocation").InnerText = ProjectLocationTextBox.Text;
- doc.SelectSingleNode("SilverJ/CurrentProjectName").InnerText = ProjectNameTextBox.Text;
- doc.SelectSingleNode("SilverJ/CurrentProjectFileName").InnerText = projectfilename;
- doc.SelectSingleNode("SilverJ/CurrentProjectType").InnerText = "ApplicationType";
- doc.Save(defaultprojfilepath);
-
- a = 1;
-
- this.Close();
- isfinished = true;
- }
- }
-
-
- else if (checkBox1.Checked == true)
- {
- if (JavaClassTextBox.Text != "")
- {
- String classname = JavaClassTextBox.Text;
- if (classname.Contains(".java"))
- {
- }
- else
- {
- classname = classname + ".java";
- }
-
- String javafilename = classname;
-
- if (Directory.Exists(projloc + "\\" + projfolder))
- {
- MessageBox.Show("Entered project name folder is already exists in current location", "Error............");
- }
- else
- {
-
- Directory.CreateDirectory(projloc + "\\" + projfolder);
-
- Directory.CreateDirectory(projloc + "\\" + projfolder + "\\srcclasses");
-
- Directory.CreateDirectory(projloc + "\\" + projfolder + "\\classes");
-
- String fname = projloc + "\\" + projfolder + "\\srcclasses\\" + javafilename;
- createdfilename = fname;
- String filename = fname.Substring(fname.LastIndexOf("\\") + 1);
-
-
- using (XmlWriter xmlwriter = XmlWriter.Create(projloc + "\\" + projfolder + "\\" + projectfile))
- {
- xmlwriter.WriteStartDocument();
- xmlwriter.WriteStartElement("SilverJProject");
- xmlwriter.WriteString("\n");
- xmlwriter.WriteComment("Silver-J (1.0) Java Application Project");
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("ProjectName", projectname);
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("ProjectLocationFolder", projloc + "\\" + projfolder);
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("ProjectLocationFile", projloc + "\\" + projfolder + "\\" + projectfile);
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("ProjectType", "ApplicationType");
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("MainClassFile", fname);
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("JavaClassFile", fname);
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("VisualFile", fname);
- xmlwriter.WriteEndElement();
- xmlwriter.WriteEndDocument();
- xmlwriter.Close();
- }
-
- isSaved = true;
-
- String defaultprojfilepath = Application.StartupPath + "\\files\\defaultprojloc.slvjfile";
-
-
- projectfilename = projloc + "\\" + projfolder + "\\" + projectfile;
-
- XmlDocument doc = new XmlDocument();
- doc.Load(defaultprojfilepath);
- doc.SelectSingleNode("SilverJ/DefaultProjectLocation").InnerText = ProjectLocationTextBox.Text;
- doc.SelectSingleNode("SilverJ/CurrentProjectName").InnerText = ProjectNameTextBox.Text;
- doc.SelectSingleNode("SilverJ/CurrentProjectFileName").InnerText = projectfilename;
- doc.SelectSingleNode("SilverJ/CurrentProjectType").InnerText = "ApplicationType";
- doc.Save(defaultprojfilepath);
-
- a = 2;
-
- this.Close();
- isfinished = true;
- }
- }
- }
- }
- }
First, check if project location and project name are not empty strings. Then, check whether "Create Java Class" checkbox is checked or not. If it is not checked, then just create project without .java source file.
First, create your Java project file (projectfile = projectname + ".slvjproj";). Then, we create this file and start to write that file by using XmlWriter class.
We will write Project Name, Project Location Folder, Project type tags with its read values. Once we are done with this, we need to change the contents of file defaultprojloc.slvjfile
In this file, we will save current created/opened project name, location folder, project location file, project type.
If Create Java Class check box is checked then we will perform all action as above just adding following tags and info to it.where fname is entered java class file name
- xmlwriter.WriteElementString("MainClassFile", fname);
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("JavaClassFile", fname);
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("VisualFile", fname);
I am keeping track of created project, which means if Project is created without creating a java class (which means Create Java Class checkbox is unchecked) then CheckProjectType() function return 1 otherwise it returns 2(variable a is used for this). This is for creating file, adding tabs to tabcontrol, adding projectname & filename to treeview etc. in MainForm.cs file.
Consider you have already designed you main IDE.
Now event for clicking on File->New->New Java Application Project
First show the New java application dialog form.
Download the source code.
ReadCurrentProjectFileName() function returns the string as project file name by reading file \files\defaultprojloc.slvjfile by reading tag CurrentProjectFileName.
If CheckProjectType() is 2 then we will create create a file by reading JavaClassFile tag text from current project, add tab to tabcontrol with control of texteditor,changing text of MainForm, add projectname & file names to ProjectExplorerTreeView and then we will save that file.
If njap.isFinished()==true it will call all functions that we need to change or modify the IDE such as adding files to project explorer tree view,set visibility of toolstrip buttons,changing text of FilenameToolStripLabel to current opened file name.
- WriteCurrentFileNames() this function reads all files from current opened project file name & write those file name to \files\files.slvjfile for save operation.
- CopyAllSourceFilesToSRCFolder() this function copies the all files from srcclasses to src folder without including .class file.
- UpdateWindowsList_WindowMenu() this function checks tabs in tabcontrol and add those tab texts to Window menu by creating new menu items for selecting each tab by clicking on that menu item.
What we are doing here is:
In New_JavaApplicationProject_Form.cs file,we are creating project file(<project_name>.slvjproj),writing project data to this file, creating directories (Project named directory in that classes,src,srcclasses directories).
In MainForm.cs file,we are reading created project file(<project_name>.slvjproj),
Adding tabs to tabcontrol with text of created filename,creating that file & writing that file,also adding contents to ProjectExplorerTreeview.
Where tab is MyTabPage which is going to add to tabcontrol.(see MyTabPage.cs)
See following article for a better understanding of adding tabs,
Same actions to create New Java Applet Project, only one extra content to create it, which means index.html file.
New Java Class
Now we need a class form that creates a java class.To create class a java project it must be created or opened in IDE. Following is the form that creates or adds java class to current opened project in the application.
In file New_Class_Form.cs, we are reading Class Name, also reading Modifiers,Super Class etc. Once Finish button is clicked, the tab is added to tabcontrol with same as class name by just adding .java to the end of it, and returning this filename with full path(getCreatedFileName() function).
In MainForm.cs file, In File_New_ClassMenuItem_Click() event, we will get this file name (getCreatedFileName()) , read contents from texteditor which is added to tabcontrol by New_Class_Form.cs file, create this file and write these contents to file using StreamWriter class.
Finally adding tag <JavaClassFile> & <VisualFile> with text as created filename.
Also adding <MainClassFile> tag, this tag text is read when we want to compile a file.
Download the source code to view complete code of ide.
Following is the code when File->New->Class option selected
- private void File_New_ClassMenuItem_Click(object sender, EventArgs e)
- {
- MyTabPage tb = new MyTabPage(this);
- New_Class_Form ncf = new New_Class_Form(tb, myTabControl);
- ncf.ShowDialog();
- String filename = ncf.getCreatedFileName();
- int sel = myTabControl.SelectedIndex;
-
- if (sel != -1)
- {
- var mytexteditor = myTabControl.TabPages[sel].Controls[0];
- mytexteditor.ContextMenuStrip = textEditorContextMenuStrip;
-
- if (filename != "")
- {
-
-
- if (File.Exists(filename))
- {
- MessageBox.Show("The file name you entered is already exists in the folder or already added to your project", "Error......");
- }
- else
- {
- try
- {
- StreamWriter strw = new StreamWriter(File.Create(filename));
- strw.Write(mytexteditor.Text);
- strw.Close();
- strw.Dispose();
- }
- catch
- { }
-
-
-
- if (ReadCurrentProjectFileName() != "")
- {
- String projectfilename = ReadCurrentProjectFileName();
- XmlDocument xmldoc = new XmlDocument();
- xmldoc.Load(projectfilename);
- XmlNode node = xmldoc.CreateNode(XmlNodeType.Element, "JavaClassFile", null);
- node.InnerText = filename;
- xmldoc.DocumentElement.AppendChild(node);
- xmldoc.Save(projectfilename);
-
- XmlDocument xmldoc2 = new XmlDocument();
- xmldoc2.Load(projectfilename);
- XmlNode node2 = xmldoc2.CreateNode(XmlNodeType.Element, "VisualFile", null);
- node2.InnerText = filename;
- xmldoc2.DocumentElement.AppendChild(node2);
- xmldoc2.Save(projectfilename);
- }
- }
- }
- }
-
-
- if (ncf.IsFinished() == true)
- {
- AddFilesToProjectExplorerTreeView();
- WriteCurrentFileNames();
- CopyAllSourceFilesToSRCFolder();
- UpdateWindowsList_WindowMenu();
- myTabControl_SelectedIndexChanged(sender, e);
-
-
- if (Directory.Exists(getCurrentProjectLocationFolder() + "\\srcclasses"))
- {
- String mainclassfile = "";
- using (XmlReader reader = XmlReader.Create(ReadCurrentProjectFileName()))
- {
- while (reader.Read())
- {
- if (reader.IsStartElement())
- {
- switch (reader.Name.ToString())
- {
- case "MainClassFile":
- mainclassfile = reader.ReadString();
- break;
- }
- }
- }
- }
- if (mainclassfile == "")
- {
- if (ReadCurrentProjectFileName() != "")
- {
- String projectfilename = ReadCurrentProjectFileName();
- XmlDocument xmldoc = new XmlDocument();
- xmldoc.Load(projectfilename);
- XmlNode node = xmldoc.CreateNode(XmlNodeType.Element, "MainClassFile", null);
- node.InnerText = filename;
- xmldoc.DocumentElement.AppendChild(node);
- xmldoc.Save(projectfilename);
- }
- }
- }
- }
- }
New Package
Here we will create a folder in the project folder.when aaa.bbb.ccc package name is entered then we will replace . to \,(aaa\bbb\ccc) and will create these directories in current opened project location folder and also will create java class in aaa\bbb\ccc folder.
For other options like,
- File->New->Interface, File->New->Enums
- File->New->HTML File, File->New->CSS File
- File->New->Text File, File->New->JavaScript File
- File->New->SQL File, File->New->XML File
- File->New->New File
This is the same code as creating New Java Class, just create files with their own extensions.
Open Project
(File->Open Project) This is nothing but the read file(.slvjproj) from OpenFileDialog, read contents from it, and perform actions about it such as adding application form text to project name text, adds tabs to tabcontrol by getting files by tags
<JavaClassFile>,<HTMLFile> etc. Once project is opened add Project Type, ProjectName, Project Location Folder to \files\defaultprojloc.slvjfile.
OpenFiles
(File->Open Files) This is same as above(Open Project) only to read files, add their names to ProjectExplorerTreeView, opening those files on tabs and add all file names with their full path to \files\files.slvjfile.
Save
(File->Save) For saving an opened file on tab, I am using FilenameToolStripLabel to show/store current opened file name full path on tab.
I am checking if FilenameToolStripLabel contains ‘\’ or not, if it contains it then read texteditor contents from current selected tab & write it to file from FilenameToolStripLabel.Text or here you can read files\files.slvjfile where contains all current opened project files with their full path.
Save All
(File->Save All) For this, I am reading a file \files\files.slvjfile, comparing each tab with each file name read from files.slvjfile, if it matches then setting that tab to selected, save that file by reading current set tab texteditor with filename
Load Sample Project
As every IDE provides some sample projects, here we will provide following sample projects.
First design your sample project form for selecting what kind of project want to create.
Creating sample project involves nothing but to create a Project file(.slvjproj), Project directory, creating sub directories in it(src,classes,srcclasses), creating project files(.java) in those directories, adding this created project information to \files\defaultprojloc.slvjfile & just call OpenProject function with created project file name.
These project files are located at Samples folder.
First let's define variables to store what files need to be read from Samples folder.
Here I defined notepad_files(notepad source file), notepad_2(source file name we want to create in/as project)
- String[] notepad_files = {
- Application.StartupPath+"\\Samples\\Notepad\\Notepad.slvjfile"
- };
-
- String[] notepad_2 ={
- "Notepad.java"
- };
-
- String defaultprojfilepath = Application.StartupPath + "\\files\\defaultprojloc.slvjfile";
-
-
- public String ProjectLocaionFoder()
- {
- String projectfolder = "";
- using (XmlReader reader = XmlReader.Create(defaultprojfilepath))
- {
- while (reader.Read())
- {
- if (reader.IsStartElement())
- {
- switch (reader.Name.ToString())
- {
- case "DefaultProjectLocation":
- projectfolder = reader.ReadString();
- break;
- }
- }
- }
- }
-
- return projectfolder;
- }
-
-
- String selected_item = listBox1.SelectedItem.ToString();
- String projectfolder = ProjectLocaionFoder();
listBox1 is the control added to form (see above image).selected_item contain selected sample project name and projectfolder contains users project folder.
- if (selected_item == "Notepad")
- {
- project_name = "Notepad";
-
- String directory = projectfolder + "\\Notepad";
- String projectfile = "Notepad.slvjproj";
-
- if (Directory.Exists(directory))
- {
- MessageBox.Show("The selected project is already exists in current location", "Error............");
- }
-
- else
- {
-
- Directory.CreateDirectory(directory);
-
- if (Directory.Exists(directory))
- {
- project_folder = directory;
-
- project_file = directory + "\\" + projectfile;
- }
-
- if (File.Exists(notepad_files[0]))
- {
- String filename = notepad_files[0];
- String fname = filename.Substring(filename.LastIndexOf("\\") + 1);
- fname = fname.Remove(fname.Length - 9);
- fname = fname + ".java";
-
- if (Directory.Exists(directory))
- {
- String src = directory + "\\src";
- String srcclasses = directory + "\\srcclasses";
- String classes = directory + "\\classes";
-
- Directory.CreateDirectory(src);
- Directory.CreateDirectory(srcclasses);
- Directory.CreateDirectory(classes);
-
- String content = "";
-
- content = File.ReadAllText(notepad_files[0]);
-
- if (Directory.Exists(srcclasses))
- {
- StreamWriter strw = new StreamWriter(File.Create(srcclasses + "\\" + fname));
- strw.Write(content);
- strw.Close();
- strw.Dispose();
- }
-
-
- using (XmlWriter xmlwriter = XmlWriter.Create(directory + "\\" + projectfile))
- {
- xmlwriter.WriteStartDocument();
- xmlwriter.WriteStartElement("SilverJProject");
- xmlwriter.WriteString("\n");
- xmlwriter.WriteComment("Silver-J (1.0) Java Application Project");
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("ProjectName", "Notepad");
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("ProjectLocationFolder", directory);
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("ProjectLocationFile", directory + "\\" + projectfile);
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("ProjectType", "ApplicationType");
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("MainClassFile", directory + "\\srcclasses" + "\\" + notepad_2[0]);
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("JavaClassFile", directory + "\\srcclasses" + "\\" + notepad_2[0]);
- xmlwriter.WriteString("\n");
- xmlwriter.WriteElementString("VisualFile", directory + "\\srcclasses" + "\\" + notepad_2[0]);
- xmlwriter.WriteEndElement();
- xmlwriter.WriteEndDocument();
- xmlwriter.Close();
- }
-
- }
- }
-
- is_project_created = true;
- }
- }
Here we are just performing actions the same way as we create new Java application projects with Create Java Class checkbox checked.
Compile & Run
Compiling using IDE means runnnimg commands through processes. But here to compile a java source file we don't need any window to show, we just need output of compilation such as errors & warnings. It is easy to create process. We need boolean value for whether processes are started or not for compilation.
Consider you are compiling your java programs using cmd, same as we are doing here.
e.g
cd C:\MyJava\Hello
javac.exe HelloWorld.java
Download the source code.
See following code;
Where EXE is application javac.exe with full path
WorkingDirectory is our source file directory(srcclasses)(above e.g C:\MyJava\Hello)
FileName is our main java source file which we want to compile (HelloWorld.java)
We need only one java source file to compile, you can provide more files.
The main source file is saved in the project file(.slvjproj) with tag <MainClassFile>. This file can be changed by going to Run->Main Class
- public bool Compile(String EXE, String WorkingDirectory, String FileName)
- {
- bool processStarted = false;
-
- if (File.Exists(EXE))
- {
- process.StartInfo.FileName = EXE;
- process.StartInfo.Arguments = FileName;
- process.StartInfo.WorkingDirectory = WorkingDirectory;
- process.StartInfo.CreateNoWindow = true;
- process.StartInfo.ErrorDialog = false;
- process.StartInfo.UseShellExecute = false;
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.RedirectStandardError = true;
- processStarted = process.Start();
- }
- else
- {
- MessageBox.Show("Unable to compile java file. Check your Java Path settings: Current Java Path : ");
- }
- return processStarted;
- }
-
-
- public void CompileJava(String file, String jdkpath)
- {
- String mystr = file;
- if (mystr.Contains(".java"))
- {
- mystr = mystr.Remove(mystr.Length - 5);
- }
- if (this.Compile(jdkpath + "\\javac.exe", Path.GetDirectoryName(file), Path.GetFileName(file)))
- {
- ErrorReader = process.StandardError;
- string response = ErrorReader.ReadToEnd();
-
- if (response != "")
- {
- ErrorTextBox.Text = response;
- if (showErrorDialog == true)
- {
- MessageBox.Show("" + response, "Errors");
- }
- }
- else if (response == "")
- {
- ErrorTextBox.Text = "Program Compiled Successfully.................!";
- }
- else if (response.Contains("uses or overrides a deprecated API."))
- {
- ErrorTextBox.Text = "Program Compiled Successfully.................!";
- }
- else
- {
- ErrorTextBox.Text = "Program Compiled Successfully.................!";
- }
- }
- else if (File.Exists("" + mystr + ".class"))
- {
- ErrorTextBox.Text = "Program Compiled Successfully.................!";
- }
-
-
- if(myTabControl.TabCount>0)
- {
- if(myTabControl.SelectedTab.Text.Contains(".java"))
- {
- String compilejavafilename = myTabControl.SelectedTab.Text;
- if (ErrorTextBox.Text.Contains(compilejavafilename))
- {
- int select_index = myTabControl.SelectedIndex;
- var texteditor = (TextEditorControl)myTabControl.TabPages[select_index].Controls[0];
- RichTextBox rtb = new RichTextBox();
- rtb.Text = texteditor.Text;
-
- for(int i=0;i<rtb.Lines.Length;i++)
- {
- if(ErrorTextBox.Lines[0].Contains(i.ToString()))
- {
- texteditor.ActiveTextAreaControl.TextArea.Caret.Line = i-1;
- }
- }
- }
- }
- }
- }
showErrorDialog is whether to show dialog after clicking on compile menu item. If error is found then show it, and set caret to that error line number.
You know how to run java programs on cmd.
First you to compile java source file, it will create .class file by going to the location folder. And then you can run that class file using java.exe command.
e.g
cd C:\MyJava\Hello
java.exe HelloWorld.class
Instead of using only java.exe, I am using full path of java.exe file(jdk_path\bin\java.exe) as a string.
- String projectlocationfolder = getCurrentProjectLocationFolder();
- if (Directory.Exists(projectlocationfolder + "\\srcclasses"))
- {
- if (filename.Contains(".java"))
- {
- String ffname = filename.Remove(filename.Length - 5);
- ffname = ffname + ".class";
-
- if (File.Exists(ffname))
- {
- ProcessStartInfo ProcessInfo;
-
- String javapath = "\"" + jdkpath + "\\java.exe" + "\"";
- String getfilename = filename.Substring(filename.LastIndexOf("\\") + 1);
- String fname = "";
- if (getfilename.Contains(".java"))
- {
- fname = getfilename.Remove(getfilename.Length - 5);
- }
- ProcessInfo = new ProcessStartInfo("cmd.exe", "/K" + " cd/ && cd " + workingDirectory + " && " + javapath + " " + fname);
- ProcessInfo.CreateNoWindow = true;
- ProcessInfo.UseShellExecute = true;
- Process.Start(ProcessInfo);
- }
- }
- }
ProcessInfo = new ProcessStartInfo("cmd.exe", "/K" + " cd/ && cd " + workingDirectory + " && " + javapath + " " + fname);
This line runs the program.first it starts the cmd.exe with command cd/.
Then it goes to project directory(workingDirectory(srcclasses)) by using cd.
Then it calls java.exe filename full path with .class filename.
Same for Run with Parameter option; just add those parameters after fname.
Same for Run Applet option only just to use appletviewer.exe with HTML source file.
Build Executable Jar
To create executable jar file, we need a manifest file (manifest.mf). That file contains the following contents.
- Manifest-Version: 1.0
- Ant-Version: Apache Ant 1.9.4
- Created-By: 1.8.0_25-b18 (Oracle Corporation)
- Class-Path:
- X-COMMENT: Main-Class will be added automatically by build
- Main-Class:
- Main-Class:
Where Main-Class: should contain mailclass filename(only filename without any file extension).
Created-By: is the JDK version.
To create jar file using cmd, first go to the working directory.
jar.exe cmf <manifest_file> <outputfile> <class_files>
e.g
jar.exe cmf manifest.mf Hello.jar HelloWorld.class
In Run_BuildMenuItem_Click(), I have used a textbox to paste all .class files in it and use textbox.Text as class files. It will not add .java source files.
It also adds directories to jar file by adding directory names to textbox. The process is the same as running Java programs, with just small changes in commands, inputs, & arguments.
Once the JAR file is created, we will create a build folder in project location folder & copy the created jar file there.