There is a Java class which creates a POST request and sends it to a servlet.
The main method of the class file (test) looks something like this:
public static void main(String[] args) throws IOException {
// Code logic goes here...
// No return Statement
}
This is called from a KornShell (ksh) script something like this:
retcode=`$CLK_JAVA_PATH -cp $CLASSPATH test ${PASSWORD} ${HOSTNAME} ${TOOLSET}`
if [ $? != "0" ];then
echo "ERROR:
echo "${retcode}"
else
echo "${SCRIPT} Success"
fi
retcode always has the value "2" independent of if the code fails or succeeds.
My question is since the return type of my main method is "void" why is the code returning some value?
The return value of a Java application is not the return value of it's main method, because a Java application doesn't necessarily end when it's main method has finished execution.
Instead the JVM ends when no more non-daemon threads are running or when System.exit() is called.
And System.exit() is also the only way to specify the return value: the argument passed to System.exit() will be used as the return value of the JVM process on most OS.
So ending your main() method with this:
System.exit(0);
will ensure two things:
that your Java application really exits when the end of main is reached and
that the return value of the JVM process is 0
Java programs do not return an exit code back to the operating system by returning a value from main, as is done in C and C++. You can exit the program and specify the exit code by calling System.exit(code);, for example:
// Returns exit code 2 to the operating system
System.exit(2);
System.exit(0);
This returns error code 0 (everything went fine).
System.exit Doc
Use
System.exit( someNumber );
this will give your app control over the return value seen by the OS.
Your program always returns a return code after exiting. In normal programs, if you do not specify a return code, it will return zero (this includes setting the return type to void).
Java, however, likes to be special! Java won't return the return code you return at the Main method, but it'll return some return code when the JVM exits (this accounts for multithreaded programs), and will return what a System.Exit(returnCode); call specifies.
You're not getting the exit status, that's what $? contains. You're getting standard out, whatever is written to System.out.
Related
I am using below script(.sh file) to run a java code in an UNIX system,but even if Java is giving exception or terminated successfully in both cases,
exit code coming as 0, I want to return non zero exit codes from script, if Java run throwing an exception, so that I can add if-then check in script,to print success or failure messages.
#!/bin/sh
echo 'processing started -->>'
LOC=/opt/appl/Mapping/
export JAVA_HOME=/usr/java6
export PATH=/usr/java6/bin
LD_LIBRARY_PATH=/opt/appl/JARS/
export LD_LIBRARY_PATH
java -classpath $LOC/ojdbc6-11.2.0.3.0.jar:$LOC/ds35-02.00.11.jar:$LOC/log4j-1.2.17.jar:$LOC/TestClasses.jar:$LOC/db2jcc_license_cisuz-3.0.0.jar:$LOC/db2jcc_license_cu-3.0.0.jar:$LOC/db2jcc-3.0.0.jar -Xms256M -Xmx512M com.home.backfill.TestRun
I can use Try-catch in Java file and use System.exit(1) at catch block, but I am looking for any good generic approach, as My code could be very long, not sure if it would be good idea to put System.exit(1) in every catch block.
If you want something generic:
interface ExceptionAction {
public void apply() throws Exception;
}
class Catcher {
public static void invoke(ExceptionAction ea) {
try{
ea.apply();
} catch(Exception e) {
System.exit(1);
}
}
}
Then invoke by creating lambdas. (You will have to set any 'return' value to a captured local variable):
Catcher.invoke(() -> {
/// your code here
});
I would note that this is about as verbose as try/catch though, and the latter is probably more readable/colloquial.
If I understood you correctly, why don't you ran a generic function that determines what kind of code to return?
try {
//your code here
} catch(YourException e){
Handler.handle(e);
}
Where Handler.handle(e) takes an exception and determines the Integer with which to shutdown the system. At least there you have a centralized point of where the system will shutdown.
How to return to the main method and execute again the whole program? I tried the return but it just ends the execution.
This is the code:
File folder = new File("C:\my path");
File[] listOfFiles = folder.listFiles();
if (folder.isDirectory()){
if(folder.list().length>0){
System.out.println("Directory is not empty!");
}
else {
System.out.println("Directory is empty!");
return; //I need to return from the main method
}
}
else{
System.out.println("Invalid directory!");
}
I want to return to the main method if the directory is empty. I used the return but, it did not return from the start :( Any idea on how to return to the main method? Thanks :)
This is the entire main method: (Sorry if it is too long)
My goal is to check if a new file entered the directory. Then it will list the files on that directory and one-by-one it will be deleted. If there's no file from the directory, it will return to the start.
Comment correction: "//It will return to the start"
The two loops: The second loop will be executed during the execution of the first loop. (Sorry if I did not indent it). After the execution of the second loop, it will return to the first loop.
It's hard to read but I assume you want to exit the loop using return statement which is not working that way. If you want to exit a loop you have to use break.
Here's a little bit of explanation: Difference between Return and Break statements
And since it's a basic programming knowledge I advice you to go through some Java tutorial first. For example: http://www.tutorialspoint.com/java/inde
The traditional way to return all the way to the top main method, is to throw a RuntimeException (or one of its subclasses) which is caught by the main method only. Any exception handlers on the way up, must rethrow this exception after doing what they need to.
Note that your application must be written very carefully to avoid leaking resources like opened files.
Don't do everything in main method. You must extract part of your code in other declared method like:
public static void doPartOfSomething() {
...
}
public static String doOtherPartOfSomething( String path ) {
...
return "aresult"; // Stop the execution of this method
}
Please note that the second one takes a parameter and returns a result when is called (a String here)
And then, from main method (or other place), you can call this piece of code several times:
doPartOfSomething();
doPartOfSomething();
String result = doOtherPartOfSomething( "/test" );
...
Doing that, you can execute some piece of code, return and stop a part of code, and continue with another call into the calling method.
I think you should learn more about classes, objects, methods and fields in java.
I'm using LuaJ to run user-created Lua scripts in Java. However, running a Lua script that never returns causes the Java thread to freeze. This also renders the thread uninterruptible. I run the Lua script with:
JsePlatform.standardGlobals().loadFile("badscript.lua").call();
badscript.lua contains while true do end.
I'd like to be able to automatically terminate scripts which are stuck in unyielding loops and also allow users to manually terminate their Lua scripts while they are running. I've read about debug.sethook and pcall, though I'm not sure how I'd properly use them for my purposes. I've also heard that sandboxing is a better alternative, though that's a bit out of my reach.
This question might also be extended to Java threads alone. I've not found any definitive information on interrupting Java threads stuck in a while (true);.
The online Lua demo was very promising, but it seems the detection and termination of "bad" scripts is done in the CGI script and not Lua. Would I be able to use Java to call a CGI script which in turn calls the Lua script? I'm not sure that would allow users to manually terminate their scripts, though. I lost the link for the Lua demo source code but I have it on hand. This is the magic line:
tee -a $LOG | (ulimit -t 1 ; $LUA demo.lua 2>&1 | head -c 8k)
Can someone point me in the right direction?
Some sources:
Embedded Lua - timing out rogue scripts (e.g. infinite loop) - an example anyone?
Prevent Lua infinite loop
Embedded Lua - timing out rogue scripts (e.g. infinite loop) - an example anyone?
How to interrupt the Thread when it is inside some loop doing long task?
Killing thread after some specified time limit in Java
I struggled with the same issue and after some digging through the debug library's implementation, I created a solution similar to the one proposed by David Lewis, but did so by providing my own DebugLibrary:
package org.luaj.vm2.lib;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
public class CustomDebugLib extends DebugLib {
public boolean interrupted = false;
#Override
public void onInstruction(int pc, Varargs v, int top) {
if (interrupted) {
throw new ScriptInterruptException();
}
super.onInstruction(pc, v, top);
}
public static class ScriptInterruptException extends RuntimeException {}
}
Just execute your script from inside a new thread and set interrupted to true to stop the execution. The exception will be encapsulated as the cause of a LuaError when thrown.
There are problems, but this goes a long way towards answering your question.
The following proof-of-concept demonstrates a basic level of sandboxing and throttling of arbitrary user code. It runs ~250 instructions of poorly crafted 'user input' and then discards the coroutine. You could use a mechanism like the one in this answer to query Java and conditionally yield inside a hook function, instead of yielding every time.
SandboxTest.java:
public static void main(String[] args) {
Globals globals = JsePlatform.debugGlobals();
LuaValue chunk = globals.loadfile("res/test.lua");
chunk.call();
}
res/test.lua:
function sandbox(fn)
-- read script and set the environment
f = loadfile(fn, "t")
debug.setupvalue(f, 1, {print = print})
-- create a coroutine and have it yield every 50 instructions
local co = coroutine.create(f)
debug.sethook(co, coroutine.yield, "", 50)
-- demonstrate stepped execution, 5 'ticks'
for i = 1, 5 do
print("tick")
coroutine.resume(co)
end
end
sandbox("res/badfile.lua")
res/badfile.lua:
while 1 do
print("", "badfile")
end
Unfortunately, while the control flow works as intended, something in the way the 'abandoned' coroutine should get garbage collected is not working correctly. The corresponding LuaThread in Java hangs around forever in a wait loop, keeping the process alive. Details here:
How can I abandon a LuaJ coroutine LuaThread?
I've never used Luaj before, but could you not put your one line
JsePlatform.standardGlobals().loadFile("badscript.lua").call();
Into a new thread of its own, which you can then terminate from the main thread?
This would require you to make some sort of a supervisor thread (class) and pass any started scripts to it to supervise and eventually terminate if they don't terminate on their own.
EDIT: I've not found any way to safely terminate LuaJ's threads without modifying LuaJ itself. The following was what I came up with, though it doesn't work with LuaJ. However, it can be easily modified to do its job in pure Lua. I may be switching to a Python binding for Java since LuaJ threading is so problematic.
--- I came up with the following, but it doesn't work with LuaJ ---
Here is a possible solution. I register a hook with debug.sethook that gets triggered on "count" events (these events occur even in a while true do end). I also pass a custom "ScriptState" Java object I created which contains a boolean flag indicating whether the script should terminate or not. The Java object is queried in the Lua hook which will throw an error to close the script if the flag is set (edit: throwing an error doesn't actually terminate the script). The terminate flag may also be set from inside the Lua script.
If you wish to automatically terminate unyielding infinite loops, it's straightforward enough to implement a timer system which records the last time a call was made to the ScriptState, then automatically terminate the script if sufficient time passes without an API call (edit: this only works if the thread can be interrupted). If you want to kill infinite loops but not interrupt certain blocking operations, you can adjust the ScriptState object to include other state information that allows you to temporarily pause auto-termination, etc.
Here is my interpreter.lua which can be used to call another script and interrupt it if/when necessary. It makes calls to Java methods so it will not run without LuaJ (or some other Lua-Java library) unless it's modified (edit: again, it can be easily modified to work in pure Lua).
function hook_line(e)
if jthread:getDone() then
-- I saw someone else use error(), but an infinite loop still seems to evade it.
-- os.exit() seems to take care of it well.
os.exit()
end
end
function inithook()
-- the hook will run every 100 million instructions.
-- the time it takes for 100 million instructions to occur
-- is based on computer speed and the calling environment
debug.sethook(hook_line, "", 1e8)
local ret = dofile(jLuaScript)
debug.sethook()
return ret
end
args = { ... }
if jthread == nil then
error("jthread object is nil. Please set it in the Java environment.",2)
elseif jLuaScript == nil then
error("jLuaScript not set. Please set it in the Java environment.",2)
else
local x,y = xpcall(inithook, debug.traceback)
end
Here's the ScriptState class that stores the flag and a main() to demonstrate:
public class ScriptState {
private AtomicBoolean isDone = new AtomicBoolean(true);
public boolean getDone() { return isDone.get(); }
public void setDone(boolean v) { isDone.set(v); }
public static void main(String[] args) {
Thread t = new Thread() {
public void run() {
System.out.println("J: Lua script started.");
ScriptState s = new ScriptState();
Globals g = JsePlatform.debugGlobals();
g.set("jLuaScript", "res/main.lua");
g.set("jthread", CoerceJavaToLua.coerce(s));
try {
g.loadFile("res/_interpreter.lua").call();
} catch (Exception e) {
System.err.println("There was a Lua error!");
e.printStackTrace();
}
}
};
t.start();
try { t.join(); } catch (Exception e) { System.err.println("Error waiting for thread"); }
System.out.println("J: End main");
}
}
res/main.lua contains the target Lua code to be run. Use environment variables or parameters to pass additional information to the script as usual. Remember to use JsePlatform.debugGlobals() instead of JsePlatform.standardGlobals() if you want to use the debug library in Lua.
EDIT: I just noticed that os.exit() not only terminates the Lua script but also the calling process. It seems to be the equivalent of System.exit(). error() will throw an error but will not cause the Lua script to terminate. I'm trying to find a solution for this now.
Thanks to #Seldon for suggesting the use of custom DebugLib. I implemented a simplified version of that by just checking before every instruction if a predefined amount of time is elapsed. This is of course not super accurate because there is some time between class creation and script execution. Requires no separate threads.
class DebugLibWithTimeout(
timeout: Duration,
) : DebugLib() {
private val timeoutOn = Instant.now() + timeout
override fun onInstruction(pc: Int, v: Varargs, top: Int) {
val timeoutElapsed = Instant.now() > timeoutOn
if (timeoutElapsed)
throw Exception("Timeout")
super.onInstruction(pc, v, top)
}
}
Important note: if you sandbox an untrusted script calling load function on Lua-code and passing a separate environment to it, this will not work. onInstruction() seems to be called only if the function environment is a reference to _G. I dealt with that by stripping everything from _G and then adding whitelisted items back.
-- whitelisted items
local sandbox_globals = {
print = print
}
local original_globals = {}
for key, value in pairs(_G) do
original_globals[key] = value
end
local sandbox_env = _G
-- Remove everything from _G
for key, _ in pairs(sandbox_env) do
sandbox_env[key] = nil
end
-- Add whitelisted items back.
-- Global pairs-function cannot be used now.
for key, value in original_globals.pairs(sandbox_globals) do
sandbox_env[key] = value
end
local function run_user_script(script)
local script_function, message = original_globals.load(script, nil, 't', sandbox_env)
if not script_function then
return false, message
end
return pcall(script_function)
end
The following code segment:
private class ConnectionControl implements Runnable
{
public void run()
{
while( true )
{
if( !cnnct.isInMsgEmpty() )
System.out.println( "Incoming message: " + cnnct.getInMsg().getPayloadString() ) ;
}
}
}
Works when I run it in eclipse debugger and place a breakpoint at the System.out line. However, if I run it normally I don't get the "Incoming message..." output.
Any thoughts on why this would be or how even to debug it???
Ahh figured it out... had a deadlock situation going on where two threads were using the same resource. Thanks for your help guys!
Cheers!
There are multiple ways to invoke Java code, depending on where you need it.
What you have shown is not enough to be self-standing, and should cause an error if you try to invoke it as an applet or a java application (java .... ConnectionControl). It may be that Eclipse can invoke a Runnable - I have not seen it though.
Try
making the class public
add a static main method making it a Java application
put a message in the start of the main method so you can see it is invoked
You're already using System.out.println for your program output. Add some sysouts that output where you are in the code and the status of various variables.
I don't know how this is being called but from the code I see your if condition is always evaluating false.
How to detect that code is running inside eclipse IDE
I am not aware of a generic way to get this kind of information.
One suggestion:
When you start a Java program (or a web server) inside Tomcat, simply add an argument that will indicate that this program is launched by Eclipse.
You can do that by opening the "Open Run Dialog" ("Run" menu), then select your type of application and add in the "Arguments" tab a -DrunInEclipse=true.
In your Java code, you can check the value of the property:
String inEclipseStr = System.getProperty("runInEclipse");
boolean inEclipse = "true".equalsIgnoreCase(inEclipseStr);
This way, if the program is not running inside Eclipse (or unfortunately if you forgot to set the property) the property will be null and then the boolean inEclipse will be equal to false.
Although I agree that having the code detecting a single IDE as the dev env is not an optimal solution, the following code works.
Like others proposed, using a flag at runtime is better.
public static boolean isEclipse() {
boolean isEclipse = System.getProperty("java.class.path").toLowerCase().contains("eclipse");
return isEclipse;
}
1) Create a helper method like:
public boolean isDevelopmentEnvironment() {
boolean isEclipse = true;
if (System.getenv("eclipse42") == null) {
isEclipse = false;
}
return isEclipse;
}
2) Add an environment variable to your launch configuration:
3) Usage example:
if (isDevelopmentEnvironment()) {
// Do bla_yada_bla because the IDE launched this app
}
Actually the code is not being run inside Eclipse, but in a separate Java process started by Eclipse, and there is per default nothing being done by Eclipse to make it any different than any other invocation of your program.
Is the thing you want to know, if your program is being run under a debugger? If so, you cannot say for certain. You CAN, however, inspect the arguments used to invoke your program and see if there is anything in there you do not like.
If your workspace matches some pattern like "/home/user/workspace/Project" you can use the code below:
Boolean desenv = null;
boolean isDevelopment() {
if (desenv != null) return desenv;
try {
desenv = new File(".").getCanonicalPath().contains("workspace");
}
catch (IOException e) {
e.printStackTrace();
}
return desenv;
}
A more generic and precise way, that can be used on any IDE would be loop at:
ManagementFactory.getRuntimeMXBean().getInputArguments()
looking for "-Xdebug" || (starting with) "-agentlib:jdwp=".
I came with this from #saugata comment here.
This is excellent if you want to throw a conditional exception preventing the application from simply exiting. Use a boolean like "ideWait" and add it to Eclipse watch expressions as ideWait=false, so whenever you stop at that throw, and "drop to frame" you can continue debugging happily (I mean it!)
I don't think there is any way to do this. But what I would suggest is just a command line argument such as 'debug'. Then in your main method just do
if (args.length > 0 && args[0].equalsIgnoreCase("debug")) {
// do whatever extra work you require here or set a flag for the rest of the code
}
This way you can also get your extra code to run whenever you want just by specifiying the debug parameter but under normal conditions it will never execute.
This might work if your alternative execution work flow provides a different set of dependencies:
boolean isRunningInEclipe = false;
try {
Workbench.getInstance();
isRunningInEclipe = true;
} catch (NoClassDefFoundError error) {
//not running in Eclipse that would provide the Workbench class
}
You could detect if you're inside a JAR or not, as per Can you tell on runtime if you're running java from within a jar?
Eclipse will run your app from the classes/ dir, whereas most of your end users will be running the app from a JAR.
System.out.println("Is my parent eclipse[.exe]? " +
ProcessHandle.current()
.parent()
.flatMap(parent -> parent.info().command())
.orElse("")
.matches("^.*eclipse(\\.exe)?$"));
You may try something like this:
if (ClassLoader.getSystemResource("org/eclipse/jdt/core/BindingKey.class")!=null){
System.out.println("Running within Eclipse!!!");
} else {
System.out.println("Running outside Eclipse!!!");
}