I'm starting out to learn Lua script usage in Java via LuaJava; my IDE is Eclipse.
But when I execute this simple Hello World snippet there is no output in the Eclipse console.
Took the code snippet from here
package com.cpg.lua;
import org.keplerproject.luajava.LuaState;
import org.keplerproject.luajava.LuaStateFactory;
public class Hello
{
public static void main(String[] args)
{
LuaState luaState;
luaState = LuaStateFactory.newLuaState();
luaState.openLibs();
luaState.LdoFile("hello.lua");
luaState.close();
}
}
hello.lua
function hello()
print("Hello World from Lua!")
end
hello()
But the script beneath works perfectly well.
hello2.lua
print("Hello World from Lua!")
Anyone know why the script with the function definition inside does nothing when called from Java but when executed through the console works perfectly?
I haven't tried or seen a function called like that. But you can call the hello() function from the Java like this:
LuaState l = LuaStateFactory.newLuaState();
l.doFile("main.lua");
l.getGlobal("hello");
l.call(0, 0);
Related
For example I want to print statement like -
System.out.println("Hello");
How can the above line be printed on console without using the public static void main(String arg[]); in the class.
You will soon be able to do that with JShell, this allow you to type java expression in a shell to get the result, without having to compile the program. Here is a simple example from JShell - Java 9 interpreter (REPL) - Getting Started and Examples
> jdk-9/bin/jshell -v
jshell> void helloJShell() { System.out.println("hello JShell"); }
| created method helloJShell()
jshell> helloJShell();
hello JShell
You could then simply do
jshell> System.out.println("hello");
I didn't get time to try this yet but it look as simple as this.
I built this project on NetBeans, and it runs fine from there. I can't get it to run on Shell.
The directory is .../AStarVacuum/src/astarvacuum
astarvacuum's contents are:
AStarVacuum.java Node.java VacuumUtils.java Grid.java
AStarVacuum.class Node.class VacuumUtils.class Grid.class
The main function is in AStarVacuum. The other java class files are also part of this project.
When I enter the command "java AStarVacuum" nothing happens other than shell lagging for a bit if I enter any other commands. I've waited without doing anything for it to do something for 30 minutes or so, even, but it gives me not output whatsoever.
Here is the code for the class containing main:
//package astarvacuum; from netbeans, commented out for shell
public class AStarVacuum {
public static void main(String[] args) {
...
}
}
And, here is the code for another class. The others were pretty similar:
//package astarvacuum;
import java.util.ArrayList;
import java.lang.Thread;
public class Node {
...
}
I'm kind of a newb at this; I usually use ides, but my professor wants me to submit this to a server and be able to run this from shell.
Any help would be appreciated.
I am new to Java. I wrote a simple program that prints "Hello World". My program compiled but did not run but gave me this exception:
Exception in thread main java.lang.NoClassDefFoundError:Hello wrong name : main hello
My program is like so:
package main;
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
My program is in: \main\Hello.java
I searched so much and compiled in different ways but i don't understand what the problem is. Can anyone help me?
You should have the Hello.java under main directory as per the package definition.
So do the following.
d:>mkdir main
d:>move Hello.java main
d:>javac main\Hello.java
d:>java main.Hello
Which would print Hello World. This is because javac will output the .class file next to .java file by default.
If you don't want this behaviour or if you don't want to move the .java file, then you can also mention where the output classes needs to go.
d:>javac -d . Hello.java
This would create the Hello.class automatically under main directory as per the package definition in relevance to the current directory. Hence,
d:>java main.Hello
Which would also print Hello World
You can learn more about how to compile java source code here
I want to execute java main class main.java by python using subprocess.Popen(). main.java takes 3 args.
I wonder how to do it? For example I have a HelloWorld.java class:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!" + args[0]);
} }
I tried to call it in python using the command:
print (subprocess.Popen('java C:/Users/testing/Hello.Main sayHello',
shell=True, stdout=subprocess.PIPE).stdout.read())
where 'sayHello' is the string args I want to pass in. It said
Error: Could not find or load main class C:.Users.testing.Hello.Main
Thanks
You may run your java file with extension .class the following way:
java your.path.Main arg1 arg2
where,
java - command, which runs the Java interpreter
your.path.Main - full name of your class (without .class)
arg1 arg2 - the arguments (written by spaces or between ")
Further, when you formatted this line, it transmits in subprocess.Popen() as argument.
subprocess.Popen('java your.path.Main arg1 arg2')
I'm not Python programmer, because I advice you to read documentation about this method.
I'm just starting Java ... again.
I just made a simple program
class first
{
public static void main()
{
System.out.println("Hello!");
}
}
This runs perfectly fine in BlueJ but it gives an error during run-time when running from command prompt.
This is the error
Exception in thread "main" java.lang.NoSuchMethodError: main
It's because I didn't give String args[] in the main parameter list
Till now, I used to give it subconsciously. I know that the string array contains all the parameter values when running but then why is it running in BlueJ?
(BlueJ is a student-friendly Java editor and compiler)
Your program is valid and will compile to the same thing whether you compile from BlueJ or from the command line.
However, blueJ will let you run any static method in a class (so you can test your functions) where as the command line java command will (only) look for a special main method to run. This main method tages a String array with all the command line parameters and your program should look like this even though you don't use these command line parameters:
class first
{
public static void main(String[] args)
{
System.out.println("Hello!");
}
}