Java ProcessBuilder Debugger Netbeans - java

I am having trouble with the deeper layers of the JVM and its debugging functionality.
What I am trying to do is start a separate java program using ProcessBuilder and let it communicate with my main process. All works fine unless I add the command
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=1044",
to the ProcessBuilder.
Class toExecute = ExampleSimulationController.class;
String javaHome = System.getProperty("java.home");
String javaBin = javaHome
+ File.separator + "bin"
+ File.separator + "java";
String classpath = System.getProperty("java.class.path");
String className = toExecute.getCanonicalName();
ProcessBuilder builder = new ProcessBuilder(javaBin, "-cp",
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=1044",
classpath, className);
builder.redirectErrorStream(true);
In adding this line to the ProcessBuilder (with the intention to add debugging functionality to the subprocess as described, for example, here: What are Java command line options to set to allow JVM to be remotely debugged?
I get an exception when trying to read as follows:
BufferedReader mainProcessConsoleOutput = new BufferedReader(new InputStreamReader(mainSimulation.getInputStream()));
and further down:
if(!(line = mainProcessConsoleOutput.readLine()).equals("someText"))
The exception is as follows:
Main Process: Exception in thread "main" java.lang.NoClassDefFoundError: /Users/...[path].../build/classes
Main Process: Caused by: java.lang.ClassNotFoundException: .Users.[same_Path].build.classes
Main Process: at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
Main Process: at java.security.AccessController.doPrivileged(Native Method)
Main Process: at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
Main Process: at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
Main Process: at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
Main Process: at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Exception in thread "main" java.lang.NullPointerException
at [PacketStructure].SimulationController.main(SimulationController.java:66)
I am using Netbeans as IDE and know about "Attaching a Debugger" and giving it the same port as specified in the command I pass to the ProcessBuilder. However, I don't know when I have to do this - before I specify a breakpoint in the subprocess? Afterwards? So far I did not find any indication that my subprocess is communicating with a debugger in any way.
What seems suspicious to me as well is the fact that the exception is thrown when I try to read from subprocess' stream - and not someplace earlier.
I do use ObjectInputStream and ObjectOutputStream to pass serialized data from one process to the other, but since I cannot debug the subprocess I don't know if that is a potential source of the problem.
I use MacOs.
Since the solution of this problem lies beyond my knowledge of the Java Magic, please help me in solving this riddle.
Thanks,
M

The classpath value must immediately follow the classpath argument.

After a lot of time spent trying to solve the problem I finally did:
ProcessBuilder builder = new ProcessBuilder(javaBin,"-agentlib:jdwp=transport=dt_socket,address=localhost:8000,server=n,suspend=y","-cp", classpath, className);
By adding the "-cp" command after the "-agentlib" command apparently the classpath is matched correctly with the className.
Should I have known that the order in which commands are passed is important?
Thx

Related

How to provide the -Dlogback.configurationFile=./logback.xml option in nodejs spawn method

I have angular2-node.js application. I am executing a jar file through the node server.
Jar execution is happening fine but it's using the logback.xml present in the jar file.
Node js code:
app.get('/report/:parameter1/:parameter2', function(req, res) {
var fileName = path.join(__dirname, '..', 'javaFile', 'xyz.jar');
spawn('/usr/bin/java', ['-jar ', fileName, parameter1 , parameter2, '&'],{
stdio : ['ignore', out, err],
detached : true }).unref();
data = '{response: Success}';
res.status(200).json(data);
res.end();
});
I want to refer the different logback.xml file for jar execution while running the jar from UI. So, i tried the below code:
spawn('/usr/bin/java', ['-jar -Dlogback.configurationFile=./logback.xml', fileName, cacheName , cacheType, '&'],{
stdio : ['ignore', out, err],
detached : true }).unref();
But, it also didn't work and throw the below error:
Unrecognized option: -jar -Dlogback.configurationFile=./logback.xml
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
I am new to node js. I searched the web but couldn't get an answer.
Is there any way to provide the logback.xml file dynamically in node.js code something like we do in shell script like below:
nohup java -jar -Dlogback.configurationFile=./logback.xml xyz.jar
Can anyone provide any solution for this.
The args arguments is <string[]>, so you should split the multiple args into multiple elements of the array, like you've done for the other arguments. You can check the signature of the method here.
Try,
spawn('/usr/bin/java', ['-jar', '-Dlogback.configurationFile=./logback.xml'], ....

Error when trying to execute a command in java [duplicate]

This question already has answers here:
How to use "cd" command using Java runtime?
(8 answers)
Closed 9 years ago.
I am trying to run a java file using the terminal but from java. Meaning, I'll run the command using java.
I am trying to execute the command 'cd /Users/apple/Documents/Documents/workspace/UserTesting/src ' that redirects to the following directory and then execute the command 'ls' that lists all the files in the current directory
I am using this method to run the Java file 'NewFile.java'
try {
String line;
Process p = Runtime.getRuntime().exec( "cd /Users/apple/Documents/Documents/workspace/UserTesting/src" );
Process p2 = Runtime.getRuntime().exec( "ls" );
BufferedReader in = new BufferedReader(
new InputStreamReader(p2.getInputStream()) );
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
catch (Exception e) {
// ...
}
The output
Directly using the terminal -> It gives 'NewFile.java'
Using this method using Java -> It always give 'bin' and 'src' for whatever command given to p2
Here are several trials
Apples-MacBook-Pro:~ apple$ cd
/Users/apple/Documents/Documents/workspace/UserTesting/src
Apples-MacBook-Pro:src apple$ java NewFile 5 90 35 45 150 3
Reichweite---- nach blase art
3 5 35 45 90 150
Apples-MacBook-Pro:src apple$ java /Users/apple/Documents/Documents/workspace/UserTesting/src/NewFile
Exception in thread "main" java.lang.NoClassDefFoundError:
/Users/apple/Documents/Documents/workspace/UserTesting/src/NewFile
Caused by: java.lang.ClassNotFoundException:
.Users.apple.Documents.Documents.workspace.UserTesting.src.NewFile at
java.net.URLClassLoader$1.run(URLClassLoader.java:202) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:190) at
java.lang.ClassLoader.loadClass(ClassLoader.java:306) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at
java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Apples-MacBook-Pro:src apple$ java
/Users/apple/Documents/Documents/workspace/UserTesting/src/NewFile.java
Exception in thread "main" java.lang.NoClassDefFoundError:
/Users/apple/Documents/Documents/workspace/UserTesting/src/NewFile/java
Caused by: java.lang.ClassNotFoundException:
.Users.apple.Documents.Documents.workspace.UserTesting.src.NewFile.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:190) at
java.lang.ClassLoader.loadClass(ClassLoader.java:306) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at
java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Apples-MacBook-Pro:src apple$
Blockquote
So, it seems the problem you're having is that you don't understand why you get different results when you invoke the program in different ways.
Here's what's going on: Runtime.geRuntime().exec() creates a new process, which is a child of the parent. Every process has its own working directory; when you fork a new process, it inherits the working directory of the parent. Invoking cd will then change the working directory of the current process (and this is a shell builtin, but ignore that for now and I'll get to it later).
So what you're doing is this:
Parent
-> create child 1 -> change working directory of child 1
-> create child 2 -> invoke "ls"
Note that child 2 will inherit the working directory of its parent. It won't know anything about the working directory of child 1. So depending on the working directory of the process that is invoking this method (in your case, either the terminal or...I don't know, your JDK install?) you will get different results.
If you want the same results every time, you could do something like this:
Process p = Runtime.getRuntime().exec( "ls /Users/apple/Documents/Documents/workspace/UserTesting/src" );
And if you want to be able to exec your program from anywhere, just use the full path:
Process p = Runtime.getRuntime().exec( "java /Users/apple/Documents/Documents/workspace/UserTesting/NewFile" );
(assuming, of course, that you have already used javac to build NewFile.class in that directory, and that you have the right permissions to execute it.)
Re: cd, as I mentioned before this is a command that's built into your shell. When you invoke the command using exec in this way, it is likely failing. You can check on that by reading standard error using the getErrorStream() method of Process.

Exception in thread "main" java.lang.NoClassDefFoundError: =

I created a new maven project in Eclipse and on runtime I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: =
Caused by: java.lang.ClassNotFoundException: =
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: =. Program will exit.
In other threads the class is mentioned where the problem occurs but here it simply says nothing.
The code is also used in a different project (with slight tweaks in terms of calling a method) but the rest of it is same.
If anyone can help me resolve this issue..it will be highly appreciated.
It looks like something is passing in = as the class name. It doesn't say nothing - it says =.
For example, when I run:
java =
I get:
Error: Could not find or load main class =
There's no colon, but it's otherwise the same.
Look at where you're trying to specify the class name, and see whether there's a stray = around. For example, suppose you had:
java -Dfoo = bar ClassName
instead of
java -Dfoo=bar ClassName
You'd see the same thing. I'm not familiar with Maven, but if you ever specify a set of arguments in it, I'd look at that part of the configuration file.
Deleting the workspace worked for me.

Running a command-line operation from within Java

I built a very simple program to test running a command line operation separate of Java. That is: later I want to be able to modify this code from using "move" to any other command I can enter into the command line (including calling other, non-Java, software).
I did search and read probably two dozen answers, but they all either suggested I was trying this correctly, were irrelevent to my simple test or proposed other solutions which did not work (like using the .exec(String[]) method instead of .exec(String) - same result!).
Here is my code:
import java.io.IOException;
public class RunCommand {
private static final String PATH_OUT = "C:\\Users\\me\\Desktop\\Temp\\out\\";
private static final String FILE = "sample.txt";
private static final String PATH_IN = "C:\\Users\\me\\Desktop\\Temp\\in\\";
public static void main(String[] args) {
try {
String command = "move "+PATH_IN+FILE+" "+PATH_OUT;
System.out.println("Command: "+command);
Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is what I see output when I run:
Command: move C:\Users\myingling\Desktop\CDS\Temp\in\sample.txt C:\Users\myingling\Desktop\CDS\Temp\out\
java.io.IOException: Cannot run program "move": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at RunCommand.main(RunCommand.java:13)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
Note that when I copy/paste the command into a command prompt window, the file moves successfully though.
What am I missing? All the other questions I've read seem to indicate this should work.
Thanks!
EDIT Works now, thanks for the help everyone! It's annoying that it's hidden the way "move" is a parameter of cmd.exe. I wish they had made it so if it worked when copy/pasted it worked when you called the .exec() method. Oh well.
The "move" command is part of the cmd.exe interpreter, and not a executable by itself.
This would work:
cmd.exe /c move file1 file2
Try this:
Runtime.getRuntime().exec("cmd.exe /c move "+PATH_IN+FILE+" "+PATH_OUT);
In Windows, unlike UNIX, move isn't a separate program. You need to involke the command processor CMD with move as an argument. Read the command line help on CMD, there's a flag you have to set.
move isn't actually a program, its a shell built-in command. Use something like:
String command = PATH_TO_SYSTEM32 + "\\cmd.exe /c move \""+PATH_IN+FILE+"\" \""+PATH_OUT + "\"";
Good practice is to always use absolute paths for external programs. (Well, good practice in this case would be to use Files.move or an equivalent instead of a platform dependent shell call)

Compiling a class using Java code using process

I have this piece of code that compiles a class called tspClassName, when I compile using this code:
Process compileProc = null;
try {
compileProc = Runtime.getRuntime().exec("javac -classpath ."
+ File.separator + "src" + File.separator
+ File.separator + "generated." + tspClassName + ".java -d ." + File.separator + "bin");
// catch exception
if (compileProc.exitValue() != 0)
{
System.out.println("Compile exit status: "
+ compileProc.exitValue());
System.err.println("Compile error:" +
compileProc.getErrorStream());
it outputs this:
"Compile exit status: 2
Compile error:java.io.FileInputStream#17182c1"
The class tspClassName.java compiles without errors otherwise, so I am guessing it has to do with the path,and in my eclipse project, the tspClassName.java resides in package homework4.generated inside src, is there something wrong with the path that I use in the code?
thanks
Your Java code runs a command that looks something like this:
javac -classpath ./src//generated.ClassName.java -d ./bin
I don't think that's what you want. I think you need to change your Java code so it maybe generates something like:
javac -classpath . src/generated/ClassName.java -d ./bin
^
Note the space after the classpath (".").
You can use the javax.tools.JavaCompiler or JCI that wrap this functionality.
I recommend doing something like this:
String command = String.format(
"javac -classpath . src%1$sgenerated%1$s%2$s.java -d .%1$sbin",
File.separator,
tspClassName
);
LOG("Executing " + command);
//... exec(command) etc
... where LOG is whatever your logging framework uses to log the command to be executed. This will help debugging immensely, since it was pointed out that the command you built is ill-constructed.
Alternately you can also build the string using replace
String command =
"javac -classpath . src/generated/ClassName.java -d ./bin"
.replace("/", File.separator)
.replace("ClassName", tspClassName);
This is perhaps more readable.
On draining Process streams
OP's comment suggests that waitFor() never returns. This is likely caused by compilation errors/warnings in javac process.
From the API:
Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
You need to continuously drain the Process.getOutputStream() et.al.
See also
Java Puzzlers, Puzzle 82: Beer Blast
Related questions
Draining standard error in Java
I think the proper way to do this kind of work is programatically using the javax.tools API, not an external process:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler()
Reference:
ToolProvider.getSystemJavaCompiler()
The problem could be with the file location instead of using single value parameter for exec()
try 3 parameter method which has the command, environment and location as parameters which helps us to move to the specified location and execute the command
check 6 and 8 methods for reference
http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
Process compile = Runtime.getRuntime().exec("javac "+fname,null,new File(dir));
firstly you should use apache exec library if you want to use processes like that. Apache exec library makes things very easy.
Secondly you should print your std output and std error streams of your process which you are executing. Without them its no way to know whats being executed and what's it doing.
Thirdly, try to print the full cmd line which the process is executing. Copy that cmd line and try to run it manually. Most of the time you would find your issues this way.
And finally if your aim is just to compile a class / generate or modify a class file at runtime give this a good read and try. It has examples too. You could also try code generation / class manipulation libraries like BCEL, JavaAssist etc.
Best of luck.

Categories