How to launch a JVM from C# - c#

I would like to launch a java application from C# but am unsure of the proper way to do it. I can do it from a bat file:
java -cp ".;other_classes_location" classname
How does this get turned into C#?

What you have posted is a command line (likely executed in a command shell)
you cannot turn this into C# code
I guess you are looking for a way to execute that command using C#. In this case the answer is the below code
System.Diagnostics.Process.Start("yourPath\java.exe", "Command Line Arguments");

Use Process.Start.
Examples here.

Related

Non-Terminating Process Git Bash (C#)

As a fun little project, I am trying to use C# to operate the bash.exe provided by Git. I want the process to behave just as if I ran it in the Git Bash Application. By this, I mean I want to be able to execute command and get the output of said commands (i.e. if I enter the command "curl --version", I want to get the same output as the image here and be able to store it in a variable)
I have come very close to accomplishing this with the code here. However, with some commands, I find that the Process in C# never terminates. For example, if I try to execute the command "curl --help", I find the the Process never exits where "curl --version" did. As a quick hack, I figured out that I could fix this by changing the command to
curl --help >> output.txt
and then reading the .txt file. This does cause the command to exit and to write the correct output to the file, however, I don't like having to do this and I am sure there is a better solution to make commands of this sort exit properly. Thanks for the help!

Execute a WinDbg command through C#

I need to execute a WinDbg command through C#. To be more clear, open the WinDbg through C# in background, execute a command in the windbg command line and close the windbg application. Does C# provide any APIs for doing this ??
If you really want the GUI, just use the -c switch to pass a command to the window. An example command line to attach to Calculator and dump the stack:
windbg.exe -pn calc.exe -c "kb"
This leaves Windbg open and attached to calculator, displaying the result of running kb.
If you don't need the Windbg GUI and just need to execute a command to get the output of it, use CDB (the command-line debugger equivalent).
cdb.exe -pn calc.exe -c "kb; qd"
So here, the command in quotes after -c is executed after attaching to the process named (due to -pn) "calc.exe".
In either case, if you instead have the process ID (PID), use -p:
cdb.exe -p 1164 -c "kb; qd"
As for running it from C#, the easiest way is to start a Process and read the console output. See this answer for a ready-to-go solution.
No, C# does not have API to run or control WinDbg.
You can use general purpose Process.Start to launch WinDbg and pass script to it.
You can execute C# code from Windbg command line, and an approach is to write a plugin for Windbg. Not sure this is the approach you're after but if so here is a post how to do.
see:
https://powerdbg.codeplex.com/
it is not C#, but it is >net and may be you will find out an approach
If you are ready to change ะก# to python, see
https://pykd.codeplex.com
And at last, you can use native dlls DbgEng/DbgHlp from your managed code
Naitive debugger engine cannot be used unless you wrap it around c++/cli checkout mdbglib
https://mdbglib.codeplex.com
This is a debugger written such a fashion
You can use ClrMD for C# API
Here is the sample code to get started
C# code snippet on how to use it
// Create Debugger instance and call Execute for any Windbg Command
using (DbgEngine dbg = new DbgEngine(DumpFileName))
{
Console.WriteLine(dbg.Execute(".time"));
Console.WriteLine(dbg.Execute("~"));
Console.WriteLine(dbg.Execute(".sympath"));
}
Simple Debugger to run Windbg Commands and also query .NET CLR Runtime data in C#
https://github.com/sukesh-ak/AutoDebug

How to execute DOS command and retrieve result [duplicate]

This question already has answers here:
How To: Execute command line in C#, get STD OUT results
(17 answers)
Closed 9 years ago.
My C# application requirement is to issue dir command from command line (with J:\MyFolder> as current directory) and receive output from that command in my C# application.
I tried MSDN where issuing command line examples are there like "/c dir" but I would like to retrieve result also.
Can somebody help me with that? Thanks in advance.
http://www.dotnetperls.com/process-start
and http://www.dotnetperls.com/redirectstandardoutput
Y
Ou can use the ProcessStartInfo class to call other exes and .bats etc and redirect the outout and errors back into your c# program. However this is nasty and could be better achieved in direct code probably depending on what logic you are calling in addition to yoir example. Thinhs such as powershell and wmi and basical file and directory handling
Don't do that.
Use DirectoryInfo.
The basic idea is to start a process using "command.com" as the executable and pass the command line as a parameter as well as redirect the stdout back into your program.
I just reviewed these two links: http://www.dotnetperls.com/process-start and
http://www.dotnetperls.com/redirectstandardoutput These are reasonable descriptions of what you need to do.
It really isn't that difficult, mostly just time consuming getting all the details correct!
To test this out prior to programming invoke command.com and ensure you can use it to issue commands. I found my copy of command.com at C:\WINDOWS\system32.
Hope this helps, please ask if more questions.

IronPython sys._getframe not found

I'm currently building a program in C# which will call functions in provided python script files.
Some of these script files calls _getframe() in sys, which results in the error:
System.MissingMemberException: 'module' object has no attribute
'_getframe'
(Since IronPython doesn't have _getframe activated by default.)
I have done quite a lot of googling and found out that you can activate it in ipy.exe by providing -X:Frames as a command line option, however this doesn't solve my problem since I'm not directly using ipy.exe to execute the python code.
In this thread they mention rebuilding IronPython from source with the command line options, I downloaded the source files but have no idea how to build it with those options.
Also they mention that the options are in the official installer, I have run the installer exe several times but haven't seen a glimpse of those options there.
When creating the PythonEngine you can pass a dictionary of options; you just need to set the "Frames" and/or "FullFrames" keys in the dictionary to true:
var options = new Dictionary<string, object>();
options["Frames"] = true;
options["FullFrames"] = true;
ScriptEngine engine = Python.CreateEngine(options);
If you don't want FullFrames, just leave it out or set it to false.
A little out of the scope of the question, but meant for anyone else getting this error by invoking a Python script using the ipy.exe interpreter directly.
You can just add the argument -X:FullFrames. So for example invoke the script like
ipy.exe -X:FullFrames script.py

Run an executable present in Windows Path using C#

I'm trying to run some commands, like rails test, using a C# command line. I tried using How To: Execute command line in C#, get STD OUT results but I'll need full path to the rails executable for that to work. Is there any alternative that will find work just like the windows command line does?
If you can P/Invoke, you could locate the executable with PathFindOnPath. A quick google doesn't show a C# equivalent.
without P/Invoke, Environment.GetEnvironmentVariable("Path").Split(";") should give you a list of paths to probe.
However, this is not the entire resolution used by ShellExecute or even the console.
I believe if you have UseShellExecute set to true in the ProcessStartInfo used to start the process, it'll use the path. Haven't checked it yet - will do so when I get a chance.

Categories