-1

I have created a Java program to create a C++ file taking the file name as the user input and then after the file creation is done, the program should automatically open the created C++ file in CodeBlocks.

 System.out.println("Enter the Name of the file to be created ---");
         name =sc.next();
         File f = new File(name+".cpp");
         if(f.exists())
         {
             System.out.println("File already exists");
         }
         else
         {

             try{ 
                 f.createNewFile();
                 FileWriter fw = new FileWriter(name+".cpp");
                 fw.write("#include<bits/stdc++.h>\r\nusing namespace std;\r\nint main()\r\n{\r\n"
                 + "ios_base::sync_with_stdio(false);\r\ncin.tie(NULL);\r\n\r\nreturn 0;\r\n}");
                 fw.close();
                }
             catch(Exception e){System.out.println(e);} 
             System.out.println("!! File Sucessfully Created !!");
             try {
                  Runtime r = Runtime.getRuntime();
                  r.exec("CodeBlocks C:\\Users\\ADITYA\\Documents\\codeblocks progs\\"+name+".cpp");
                 } catch (IOException e) {
                 System.out.println(e.printStackTrace());                
                 }

         }
Biffen
  • 6,249
  • 6
  • 28
  • 36
  • 5
    and the question is? – 463035818_is_not_an_ai May 21 '19 at 10:08
  • Please ask the question properly. Tell us what isn't working, or put the StackTrace in, or tell us what you want the point of this question to be. – Yogesh Sudheer Modak May 21 '19 at 10:12
  • (OT: [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)) – Biffen May 21 '19 at 10:16
  • @YogeshSudheerModak the .cpp file is created but i expect the program to open that newly created file in codeblocks automatically , which is not happening , what should i do ?.....after creation of that file i get the output message "Something went wrong " i.e. the message in the catch block – Aditya Kulkarni May 22 '19 at 08:00

1 Answers1

0

Something like

Process p = Runtime.getRuntime().exec("path/to/codeblock/executable --file="+filePath);

command line arguments