compile a c++ program(runtime) using a java program - java

I want to create a java program which will compile and execute program written in c++. Ijust wanted to know the links or get any idea how will I do this, I want to learn by-self, but not sure from where i should start. I got a link for executing a ".exe" file, which is the part of my program, but how can I compile a C++ program through Java.
I tried to search the related stuff, but was unable to find...suggestions are appreciated...

I think you want to run a .exe file through Java.
You should try Runtime.getRuntime().exec(String command, String[] envparam, File dir) with:
command is the location of the .exe
envparam can be null
dir is the directory of your .exe
Example:
Runtime.getRuntime().exec("c:\\program files\\test\\test.exe", null, new File("c:\\program files\\test\\"));

Here's how you should use Runtime.exec() to execute a C++ compiler. You might also try its more modern cousin, ProcessBuilder:
Java Runtime.getRuntime().exec() alternatives

Related

How to run a C++ exe file from Java?

I want to open an EXE file from a Java program. I tried 2 procedures.
The program can run some programs, like NotePad++, but cannot run my C++ EXE file. I tried:
Process exec = Runtime.getRuntime().exec(file.getAbsolutePath());
ProcessBuilder processBuilder = new ProcessBuilder(file.getAbsolutePath());
but neither of the above work. No exception is thrown, and exec.isAlive = true.
Your mistake is that you took the absolute path in the first procedure.
Try using a relative path, I just tested and it worked just fine.
This also works for me (using 2 backslashs').
Runtime.getRuntime().exec("C:\\Program Files\\DDNet\\DDNet.exe");
Kind regards

Simple way to run java classfile (Terminal)

I wanted to put a java classfile up for download recently, which people could run in Terminal. It's a Minecraft command generator, so the people downloading it won't necessarily have the greatest mental capacity (I'm referring to 8-year-olds who have no idea what they're doing, of course).
Anyway, I wanted to provide a simple, single command, both for the Mac / Linux terminal and the Windows command line, that ran the classfile without any complications. The problem is, I don't want to execute it by doing /cd path, and then doing java someFolder.someClass. I just want to have a single command to open the file. If anyone could provide these commands for me, both in Mac / Linux and Windows, that would be great.
Sorry for the super long explanation :P
A jar file with a main class in the manifest would probably be the easiest thing. Then the command is java -jar myjarfile.jar.
A swing application would probably be easier as the default way of running executable jars doesn't open a command prompt (it uses javaw instead of java).
You will have to first start a terminal and then run java in that terminal, which can be a bit tricky.
How to open a command terminal in Linux?
Why not create an interface (Swing) and pack everything in a jar?

Can you run a Java program in Linux using the name only, without the "java" command?

If I am writing HelloWorld, is there a way I can run the program from any directory by just typing HelloWorld? Sort of the same way once you set up Ant, you can just run Ant from any directory?
Just for some details, we are creating a CLI based toolkit for a customer, and just curious if we can compile it, install it, and just have them run it using the toolkit name.
You can always create a shell script, call it HelloWorld and make it run java with your JAR.
You'll then need to chmod the script to make it executable, and place it somewhere in your $PATH.
The script would like something like:
#!/bin/bash
cd /path/to/helloworld
java -jar HelloWorld.jar "$#"
or
#!/bin/bash
java -jar /path/to/helloworld/HelloWorld.jar "$#"
depending on your exact requirements.
Common solution for your problem is to create a separate launcher application, which is non-java application that runs your Java program. Launcher can be written in some compilable language such as C/C++ and compiled into native executable. Also it can be written in some interpreted language such as Unix shell, perl, python etc and made executable by adding #!/path/to/interpreter line at the beginning of launcher file and setting executable flag on it. Also there are several utilities that can generate launcher for your program such as launch4j or jsmooth.
On Linux (specifically), you could use the /proc filesystem (see proc(5) man page) and its binfmt_misc (actually the /proc/sys/fs/binfmt_misc/register pseudo-file and other pseudofiles under /proc/sys/fs/binfmt_misc/) to register java as the handler for .class or .jar files. Read the Documentation/binfmt_misc.txt file in the kernel source for gory details.
Then any executable .jar file would be interpreted by java (or jexec)
I'm not sure it is worth the effort. I find that wrapping your Java program in some shell script is much more easy (and more portable, because few Linux systems actually use binfmt_misc, and your customer may need some sysadmin skills to enable it).

Java runtime exec

I am trying to do something using system exec in Java
Runtime.getRuntime().exec(command);
Surprisingly everything that is related with paths, directories and files is not working well
I don't get why and just want to know is there any alternatives?
The alternative is to use the ProcessBuilder class, which has a somewhat cleaner interface, but your main problem is probably related to how the OS processes command lines, and there isn't much Java can do to help you with that.
As noted above, cd is a shell builtin. i.e. it's not an executable. You can determine this using:
$ which cd
cd: shell built-in command
As it's not a standalone executable, Runtime.exec() won't be able to do anything with it.
You may be better off writing a shell script to do the shell-specific stuff (e.g. change the working directory) and then simply execute that shell script using Runtime.exec(). You can set PATH variables etc. within your script and leave Java to simply execute your script.
One thing that catches people out is that you have to consume your script's stdout/stderr (even if you throw it away). If you don't do this properly your process will likely block. See this SO answer for more details.
The exec() method can take three arguments. The third is the directory your subprocess should use as its working directory. That solves your "cd" problem, anyway.

Running the program with out GUI

I have a requirement in my project that I have to run external program with out GUI even if external program has GUI(like notepad.exe).This is possible in .NET. But my program is in java. I found out that java does not have this facility.
Please let me know is there any program like hstart(which only hides the console not GUI) which hides the GUI if program is given as parameter(Ex: [hstart] notepad.exe).
If you know how to do that in .NET write a little program for this yourself and call it from your java app.
Same idea as previous answer, but you can call through a vbs script.
For example, you have "run_and_hide.vbs"
Set WshShell = WScript.CreateObject("WScript.Shell")
Return=WshShell.Run(Wscript.Arguments(0),0,false)
Then execute the script from java code and pass the target program in the first argument:
Runtime.getRuntime().exec("cmd /c start run_and_hide.vbs " + "\"notepad\"");

Categories