I have a java class and a c# class. I want to run that C# class from my java class.
However I don't want to pass anything from java code to c# and also don't want anything in return from C# code, I just want to run that C# code.
I want to do something like shown in below classes
Java Class:
public void static main(String[] args){
System.out.println("Running Java code ");
// here need to call C# class
}
}
I want this code to be executed from above java program
using System;
class Program {
Console.WriteLine("Running C# code ");
}
}
You can run the C# program exe file from java code.
first compile the C#.NET program to get the Program.exe file then run the same Program.exe from java code as below:
public static void main(String[] args) throws IOException {
// TODO code application logic here
Process process;
process = new ProcessBuilder("C:\\ProjectsPath\\Program.exe").start();
}
Edit:
You can send the parameters to the exe file to be invoked by passing the arguments to the ProcessBuilder constructor as below:
Note : here im passing two argumenbts to Program.exe file Name and ID :
process = new ProcessBuilder("C:\\ProjectsPath\\Program.exe" , "Sudhakar","ID501").start();
Related
i have a c# dll file which contains a specific class that i need to use in some java application.
this is how i use it in my c# program:
using Lemmatizer; // this comes from my dll
private void Test(string inputText) {
output = (new StemmingTools()).LemmatizeText(inputText, LevelOfLemmatizer.First, false);
// StemmingTools class placed in the dll
}
how can i use the LemmatizeText function in my java program ?
i already used the jna library but it fails with instantiate the StemmingTools object from the dll.
Error:
Error: Main method not found in class app, please define the main method as:
public static void main(String[] args)
Code:
class app
{
public static void main(String[] args) {
double accounts[];
accounts=new double[100];
accounts[2]=1225.33;
System.out.println("Account 2 is overdue by $"+accounts[2]);
}
}
I am using EditPlus to run and execute this program.
If you are trying to run a class you need the main method. Add the main method to the class and put the code you are trying to execute inside the main method and it should execute. Also post the code you do have so it's easier for us to understand what you are trying to do.
public static void main(String[] args) {
//code goes here
}
There is either something broken about the tools you are using, or the process you are using. (For example, you might have made a mistake with your classpath.)
At any rate, your program works for me, as demonstrated by the following:
[steve#newbox tmp]$ cat > app.java
class app
{
public static void main(String[] args) {
double accounts[];
accounts=new double[100];
accounts[2]=1225.33;
System.out.println("Account 2 is overdue by $"+accounts[2]);
}
}
[steve#newbox tmp]$ javac app.java
[steve#newbox tmp]$ java app
Account 2 is overdue by $1225.33
[steve#newbox tmp]$
For the record, this is with Java 8 tools ... and I haven't set the CLASS_PATH environment variable. (Hence, java and javac will just be using the current directory as the classpath.)
The only other possibility I can think of is that your original code has a nasty homoglyph in (maybe) the main identifier, that causes the java command to not see the method. There is a chance that StackOverflow will silently "fix" homographs in Questions and Answers. (It certainly seems to "fix" dodgy control codes.)
I am trying to call a java script function from java code.
Here is my Java code
public static void main(String[] args) throws FileNotFoundException {
try {
/**
* To call a anonymous function from java script file
*/
ScriptEngine engine = new ScriptEngineManager()
.getEngineByName("javascript");
FileReader fr = new FileReader("src/js/MySpec.js");
engine.eval(fr);
} catch (ScriptException scrEx) {
scrEx.printStackTrace();
}
}
Here is my java script file:
(function() {
alert("Hello World !!!");
})();
But when I run main method of driver class it is giving me error as below:
Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "alert" is not defined. (<Unknown source>#2) in <Unknown source> at line number 2
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:232)
at Java6RhinoRunner.load(Java6RhinoRunner.java:42)
at Java6RhinoRunner.main(Java6RhinoRunner.java:12)
What I know is that it need some script engine to execute it.
For that I added rhino.jar file in to my class path.But this is not working.
I an not getting how to solve this error.
Please help.Thanks in advance.
alert is not part of JavaScript, it's part of the window object provided by web browsers. So it doesn't exist in the context you're trying to use it in. (This is also true of setInterval, setTimeout, and other timer-related stuff, FYI.)
If you just want to do simple console output, Rhino provides a print function to your script, so you could replace alert with print. Your script also has access to all of the Java classes and such, so for instance java.lang.System.out.println('Hello'); would work from your JavaScript script (although it's a bit redundant with the provided print function). You can also make Java variables available to your script easily via ScriptEngine.put, e.g:
engine.put("out", System.out);
...and then in your script:
out.println('Hello from JavaScript');
...so that's a third way to do output from the script. :-)
See the discussion in the javax.script package documentation, in particular ScriptEngine#put, or for more complex cases, Bindings (and SimpleBindings) and ScriptContext.
I'm trying to run project which uses fannj library, but I'm getting error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'fann_create_standard_array':
at com.sun.jna.Function.<init>(Function.java:179)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:347)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:327)
at com.sun.jna.Native.register(Native.java:1355)
at com.sun.jna.Native.register(Native.java:1032)
at com.googlecode.fannj.Fann.<clinit>(Fann.java:46)
at javaapplication9.JavaApplication9.main(JavaApplication9.java:14)
Java Result: 1
This is what I did:
I put fannfloat.dll to C:\Windows\System32
I added fannj-0.3.jar to project
I added newest jna.jar to project
here is code:
public static void main(String[] args) {
System.setProperty("jna.library.path", "C:\\Windows\\System32");
System.loadLibrary("fannfloat");
Fann fann=new Fann("D:\\SunSpots.net");
fann.close();
}
SunSpots.net is file from example package. fannfloat.dll: you can get from here.
The "#8" at the end of _fann_create_standard_array indicates that the library is using the stdcall calling convention, so your library interface needs to implement that interface (StdCallLibrary) and it will automatically get the function name mapper applied that converts your simple java name to the decorated stdcall one.
This is covered in the JNA documentation.
It was the first time I had to work with FANN and it took me some time to make it work.
Downloaded Fann 2.2.0. Extract (in my case "C:/FANN-2.2.0-Source") and check the path of the fannfloat.dll file. This is the library that we will use later.
Download fannj-0.6.jar from http://code.google.com/p/fannj/downloads/list.
The dll is compiled for 32 bit environment. So, make sure you have a 32 bit Java installed (even in 64 bit Windows).
I suppose you already have the .net file with your ANN. Write something like this in Java
public class FannTest {
public static void main(String[] args) {
System.setProperty("jna.library.path", "C:/FANN-2.2.0-Source/bin");
Fann fann = new Fann("C:/MySunSpots.net" );
float[] inputs = new float[]{0.686470295f, 0.749375936f, 0.555167249f, 0.816774838f, 0.767848228f, 0.60908637f};
float[] outputs = fann.run( inputs );
fann.close();
for (float f : outputs) {
System.out.print(f + ",");
}
}
}
This is my first post on the forum, hope all of you guys are well.
I've got a issue using JiST/SWANS, the ad hoc simulator in java within eclipse.
I managed to load the API, (as an external JAR ofcourse) but Im basically having a problem integrating the runtime of JiST within eclipse.
After running the hello world im usually getting a stackoverflowerror exception, since it may need modifications within the runtime.
import jist.runtime.JistAPI;
public class hello implements JistAPI.Entity {
/**
* #param args
*/
public static void main(String[] args) {
System.out.println("simulation start");
hello t = new hello();
t.myEvent();
}
public void myEvent()
{
JistAPI.sleep(1);
myEvent();
System.out.println("hello world, t=" + JistAPI.getTime());
}
}
the website is: http://jist.ece.cornell.edu/index.html
Thank you in advance!
Actually you need to run Main.java within jist.runtime. But before rigth click Main.java, properties, Run/Debug settings, New,Arguments and type your class name (plain name no .java needed) in Progam arguments. This will tell the jist interface to translate your code using the rewriter and run it.
Examples:
To run hello.java type "hello"
To run aodvsim.java type: "jist.swans.Main driver.aodvsim"
If there are arguments needed type them after the clas name like: "jist.swans.Main driver.aodvsim -n 25 -f 2000x2000 -a grid:5x5 -t 10,600,60"
Wilmer Arellano
How well does SWANS work? Given that the documentation and code date back to 2005, I am not sure if this is the best platform to use.