How to debug Java Stored Procedures in Oracle - java

I have an Oracle Db with stored java procedures, which I load new procedures here and then.
I would like to be able to debug these java procedures, with a same debug methodology like setting the App server in Debug mode.
is it possible? how can I do that?
Thanks

The Oracle JDeveloper has support for debugging java stored procedures. You will need to compile the java classes using the -g option so that the debugging information is generated in teh class files, i.e.
javac $JAVA_OPTS -g file_names
After that, you will need to write a dummy pl/sql package-procedure that invokes this java stored procedure. Then right click on the pl/sql procedure name and select 'Debug'.
After that you can step through the pl/sql and java code just as if you were debugging a regular java class. Since you compiled the java class using the -g option, you can watch variables, evaluate expressions etc. If you didn't use the -g option, you will still be able to step through the code in the debugger, but you will not be able to watch the variables/expressions.

I think you'll want to use JDeveloper which you can also use to debug the app server. It's not as good an IDE as, well, almost any other IDE, but it works well enough for debugging tasks.

Another option is to use the Java Logging framework.
You can then copy logging.properties, with your specified Handler into the folder ORACLE_HOME/javavm/lib

Related

How do you write vector files with the GDAL Java bindings?

I have no trouble reading files of different types using GDAL's Java bindings (version 2.2.1) via the org.gdal.ogr.ogr.OpenShared(filePath) method to create a DataSource. If I want to create a SQLite file from a DataSource, however, how would I do it?
Looking in the JavaDocs, I've found some ExportTo methods on the Geometry object that you can use to manually build GeoJSON, WKT, KML and others: http://gdal.org/java/org/gdal/ogr/Geometry.html
There doesn't seem to be anything in the Driver class that would let you use them to export a DataSource however.
Using ogr2ogr it would be as simple as running the following:
ogr2ogr -f "SQLite" ./output.sqlite3 ./input.geojson
And there is this technique for running ogr2ogr from within Java, but I would have assumed there'd be something native in the Java bindings.
I've accepted that the best way to do this is to simply include the ogr2ogr.java file from the GitHub repo in my project and wrap it in a function that makes it cleaner to access. It is almost a direct port of the original C++ GDAL file.
The one change I made was to rename the ogr2ogr main function to execute so that it didn't confuse application when I ran it.

When should I use which command to run Cypher query via java (neo4j)?

It seems that there are different command to run Cypher query on Java:
executionEngine.execute("cyper command"), session.run("cyper command"), statement.executeQuery("cypher command") (So far I could find nothing else)
(The first command comes from: https://www.tutorialspoint.com/neo4j/neo4j_cypher_api_example.htm;
the last two commands come from: https://neo4j.com/developer/java/#_the_example_project)
I thought that session.run("cyper command") should be used when there is no result to be return such as when a csv files is imported. However, this proves me wrong: How to load CSV file with cypher in java?
So when should I use which? Or can they be run interchangeably?
It depends on how you use Neo4j.
If you use it embedded, then you have access to the core API such as ExecutionEngine. This provides more information on how to use Neo4j in embedded mode.
If you run a Neo4j Server, then you have many options, one of which is to use the Bolt driver (session.run etc.). See the Drivers section for how to use this.
Then, there are frameworks such as Neo4j OGM and SDN providing convenient ways to access Neo4j. See https://neo4j.com/developer/java/

Script integration in Java application

I have a java application(runnable jar) and VB scripts which I'm using to telnet to a remote machine and executing some cmds. So, I first execute the vbs files and then run my jar(in all everything is working fine).
But, now I want to integrate scripts and my java jar such that, running the jar should first trigger the script followed by Java related task.
Few thing which I've come across are -
I cannot trigger Vbs from Java(javax.script - correct me if I'm wrong). So, possible options to rewite the script in are -
Javascript(have no idea what my Javascript file would have so that after reading it inside java class I can write it to the Socket output stream.)
PHP(I tried this using Java bridge but it gives some error saying cgi needs to installed. And, I believe it also requires PHP to be installed on the host machine before executing my jar. So I'm not going any futher with this approach.)
Long story short, I don't want to create any dependencies - I am looking for something like where I can package any external lib with my jar(if required) and use it to execute my scripts.
You can execute the VB-Script in an external command. There are a lot of resources on the internet that explain how to do that - for instance this link also explains how to start a VB-Script from within java. However I do not know if you need the output from the script within the Java. If so you'll have to listen to the outputstream of the created process. You should find an example for that as well with that link (using the processbuilder)
If you have the script packaged within your jar, I fear you'll have to unpack it to a temporary folder and execute it there.
The closest I have seen about VB script as a JVM language is in answer here.
Visual Basic or VBScript as Java Scripting Engine
Have you seen this wikipedia entry about JVM languages?
http://en.wikipedia.org/wiki/List_of_JVM_languages
Also, have you considered using Ant and using it programmatically from java?
Another option is to use groovy/Ant from Java.

Java command line "shell" with auto-complete

I need to create a Java command line to that will be invoked remotely on a server. It will need to:
Read "lines" of text from the user.
Recognise if the user presses the "tab" key to facilitate auto-complete.
Recognise if the user presses the "up/down" keys for history.
Before I go off and roll my own, Is anyone aware of a Java library that might facilitate all or part of this?
i.e. From the command line in ssh it might look like this:
bob> java -jar MyTool.jar
MyTool Started.
Please enter command:
> server1 startup
server starting...
server started
> server2 load accounts
Done
> server3 shutdown
Complete
>quit
Check out JReadline and jline2.
Update: picocli-shell-jline2 and picocli-shell-jline3 combine the strength of JLine 2 and 3 with picocli.
Picocli allows you to write commands (and subcommands) with options and positional parameters using very little code, and the library will generate JLine 2 and 3 command completers for all commands.
It uses ANSI colors and styles in the usage help message, and has many other unique features like negatable options, repeatable nested argument groups, variable interpolation and more.
Disclaimer: I am the author and therefore biased.
it seems like you're trying to ask 3 different questions at once and don't know what you really want an answer to.
accepting user input and providing auto-complete is trivial and i highly doubt you will find a generalized library for such a task
parsing complex bash-like statements sounds like something cool to have and a library may exist to do that, but i don't think it would give you much headroom to create your own set of bash-like instructions. (especially considering you say it needs to be more sophisticated than anything you could do as a bash script - which is a tall order)
parsing simple user input as if it was a command-line input or command is also rather trivial, and if this is what you are looking for, you should look at this possible duplicate: How to parse command line arguments in Java?
i recommend restructuring your question to be more specific in exactly what you are looking for and to avoid putting emphasis on the trivial task of "auto-complete" and simply accepting the users input in a text box.
Have you taken a look at the BeanShell? It doesn't act like a shell proper (like bash or csh) but it'll let you type java commands like an interpreter and you can use tab to autocomplete.
I've only used the 1.X versions of bean shell but they always open a window for you so it's not something you can run inside an existing shell.
I assume you mean something similar to the python interpreter. The reason there is not equivalent in Java is because Java needs to be compiled to bytecode before it can be executed.
If you are looking for something with good auto-complete capabilities. I would recommend eclipse or netbeans. They also compile your application automatically, allowing you to quickly run your code once you are done writing it.
Hope that helps.
Bit late to the party on this one, but I'd add Crash and Cliche to the mix.
Apache Karaf has a command shell which can be used as a library to build a command shell for other applications. You create classes to represent commands, and use annotations to specify the options to the commands. The Karaf library provides tab completion, history and line editing, and the ability to run interactively or read in files or command line arguments to execute as a script.
I found out about it here and have used it in my own project; it works quite well. I can't compare it to any of the others as I haven't used them.

How to execute sql script file in java in Unix

I have this sql script that I would like to execute in a java program. It takes on 3 parameters: dropper_id, to_char(begin_dt), to_char(end_dt). How would I do this?
The program is held on a Unix server.
The sql script is also located on the Unix server.
I think i would most likely want to execute the program by command line, but how do I execute it with parameters?
Process p = Runtime.getRuntime().exec ("psql sql_script.sql");
You can do that using JDBC and PreparedStatement, see these tutorials:
http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
http://www.commandprompt.com/ppbook/x20921
Here are some examples:
http://www.exampledepot.com/egs/java.sql/InsertPs.html
http://www.roseindia.net/jdbc/jdbc-mysql/TwicePreparedStatement.shtml
http://www.roseindia.net/jdbc/prepared-statement-example.shtml
http://www.heimetli.ch/jdbc/JDBCPreparedStatement.html
If both your Java application and the SQL are located on the same server, then you just need to load it and execute as given above. See examples here:
http://www.coderanch.com/t/306966/JDBC/java/Execute-sql-file-java
How to execute .sql script file using JDBC
Running a .sql script using MySQL with JDBC
If it's just running the SQL (i.e. the whole purpose of your application is to run that SQL and do nothing else), you may want to look at the alternative solutions such as using Ant for the task, see this:
http://ant.apache.org/manual/Tasks/sql.html
or using whatever is your database command line utility, see some examples here:
How to execute sql-script file in java?
This very much depends on your circumstances, so you would have to see what is important in your case to decide which way to go.
In general, the correct way to execute database commands in a Java application is to use a Java-based database connection using JDBC or an even higher-level ORM such as Hibernate. These are preferred for a number of reasons, most importantly because they can report and deal with error paths far more easily than an external command.
However, if you have a reason not to use JDBC, you can use ProcessBuilder as described in this question How To Execute Native Commands In Java?

Categories