How to run Javah from Eclipse - java

So I'm trying to run the 'javah' tool on a compiled .class file in Eclipse, but I have no idea how to do it. The examples I found simply said something along the lines of 'run javah on your class...' but I don't really know where I'd find such a command line in Eclipse.
If someone can give me a set of idiot proof instructions get this done in Eclipse, I'd be grateful.
Thanks :)

AFAIK Eclipse does not integrate javah by default. You have to set it up as external tool yourself.
Create a new external tool
Set the path to javah (on linux this
was /user/bin/javah)
Set the working dir to ${project_loc}/bin/ where bin is your Projects output directory
Add ${java_type_name} to the arguments
With this setup you can call javah as external tool on any java file in the Package explorer.
The generated header files currently land in the bin dir, this can be changed by adding the -d option.

Here is a sample command line:
javah -classpath /path/to/project/classes com.mycompany.MyClass
/path/to/project/classes - This is the 'Output folder' from the Source tab of Java Build Path properties page for you project.
It may be relative to the directory from where you are running javah.
You may use -verbose flag to see more details about what's going on.

Related

Where should vecmath.jar go in MacOS(11.5.1) [duplicate]

This question already has answers here:
Import javax.vecmath
(6 answers)
Closed 1 year ago.
Preamble: So this all started with just trying to use javax.vecmath.Vector2d. I didn't have javax.vecmath so I spent a bit of time trying to get it, found that I needed to download Java3D.
After a long time of trying to download Java3D for Java (version 16.0.2), I eventually got it together with the vecmath.jar file landing in /Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home/lib/ext. This got rid of the error: package javax.vecmath does not exist error message.
Then, I got the message
<JAVA_HOME>/lib/ext exists, extensions mechanism no longer supported; Use -classpath instead.
.Error: Could not create the Java Virtual Machine.
this also wasn't letting me use any java commands in shell.
A bit of research and I concluded the solution to be moving (via Finder select and drag) j3dutils.jar, vecmath.jar, and j3dcore.jar over to lib and just deleting the lib/ext directory. I have gotten rid of the <JAVA_HOME>/lib/ext exists problem but back to error: package javax.vecmath does not exist.
I don't even know what to do know. I just want to use javax.vecmath. Am I going about this the totally wrong way? How can I get this to work?
Okay, I figured it out.
How to use javax.vecmath in Mac OS(11.5.1) with Java(16.0.2)
I am giving a description that sort of includes why I do things, skip to the TLDR at the bottom if you just want an answer.
Step 1: Download the latest version of Java3D
This contains vecmath, along with j3dcore and j3dutils. It will download a .zip file. Unzip the file and it will expand into a new directory with another .zip file inside, j3d-jre.zip. Unzip j3d-jre.zip and it will expand into a directory lib. Inside lib will be a subdirectory, ext, with three .jar files inside: j3dcore.jar, j3dutils.jar, and vecmath.jar. You can put these literally anywhere, just make sure you keep track of their location (I put them in ~/Library/Java/Extensions, this location is on the hard drive and will need an admin password to do anything–use
sudo unzip /path/to/j3d-jre.zip
if you are doing things in shell). You CAN put the ext directory in JAVA_HOME/lib/ but after Java 6, this will cause a problem.
Step 2: Change CLASSPATH
Java has no idea how to find vecmath.jar so you have to specify it.
Option 1: Specify CLASSPATH with every shell command
The simplest version is using
javac -cp ".:/path/to/vecmath.jar:" MyMainProgram.java
to compile and
java -cp ".:/path/to/vecmath.jar:" MyMainProgram
to run the program (you can also replace -cp with -classpath and it will do the same thing)
This option won't ever destroy your CLASSPATH but you also have to include the -cp command every time you compile and run a program that imports javax.vecmath.
Option 2: Specify CLASSPATH with every new terminal window
A little more lasting than -cp, you can define CLASSPATH such that any changes will only take place in that terminal window. Use this form:
export CLASSPATH=".:/path/to/vecmath.jar:"
Now when you call
javac MyMainProgram.java
java MyMainProgram
Java will see that CLASSPATH is .:/path/to/vecmath.jar and everything will compile and run without adding the -cp command.
The main downside of this option is that if you update CLASSPATH again, you have to remember to add the previous CLASSPATH (which you can see at any time with echo $CLASSPATH)
Option 3: Permanently add CLASSPATH to terminal
Enter the following into terminal:
open ~/.bash_profile
this will open a window that may or may not have code in it. Regardless of any pre-existing code, scroll to the bottom and add
export CLASSPATH=".:/path/to/vecmath.jar:"
This option holds the CLASSPATH in all terminal windows forever or until you change it (using any method above).
TLDR
Download Java3D for macOS
Unzip java3d-1_5_1-macosx.zip and open the directory it creates
Unzip j3d-jre.zip and open the new directory /lib/ and the subdirectory /lib/ext/
Move vecmath.jar, j3dcore.jar, and j3dmath.jar to ~/Library/Java/Extensions (this requires an admin password) or any other location
Run the following line in terminal:
open ~/.bash_profile
Go to the bottom and add the following:
export CLASSPATH="/path/to/vecmath.jar:$CLASSPATH"
import javax.vecmath.* to any .java program you want
The jar file can go where you want, moving it to your project's lib folder is good. The real issue is you need your classpath to point to it.
Here is a full explanation.
If you are running from the command line you don't need to set the classpath variable, you can provide it in the java command. It would be something like this:
java -cp lib/vecmath.jar Example
This assumes that the program you are working on has been compiled into a class file named Example.class. If you main method is in a package you will need to fully qualify the classname so it might look like:
java -cp lib/vecmath.jar com.demo.Example
You can list multiple jar files on the classpath, separated by a colon (:).
You can also ask for help in the command line by invoking:
java -h

Compile and Run java program in linux with path to the .java file and path to external jars

Yesterday I solved a problem with an answer here on stackoverflow. But I ended up with another problem, I will try to be clear:
I have a project folder in the /home/demo/Desktop/xlsToCsv/ directory where inside of it is the java file "xlsToCsv.java" and another directory with the external jars that I need in /home/demo/Desktop/xlsToCsv/jars.
Now I need to compile and run my program. Yesterday I ran a command that assumed that I was already inside of /home/demo/Desktop/xlsToCsv/, and the commands were:
javac -cp ".:./jars/*" xlsToCsv.java
java -cp ".:./jars/*" xlsToCsv
The problem was solved and I was able to run my program without any problems. But, my program was supposed to run from the root directory, ie, the directory where it is when I open the linux terminal without the need to make a "cd" command.
So, when I open the terminal the path to the .java file is:
/home/demo/Desktop/xlsToCsv/
And the path to jars folder is:
/home/demo/Desktop/xlsToCsv/jars/*
Can someone explain to me what I have to do, and the reason so? Because more that run the program, I want to know the reasons and understand the classpath mechanism in java.
Avoid using relative classpath. and instread of "./jars/" use the absolute path "/home/demo/Desktop/xlsToCsv/jars/"
EDIT:
javac -cp "/home/demo/Desktop/xlsToCsv/jars/*" /home/demo/Desktop/xlsToCsv/xlsToCsv.java
java -cp "/home/demo/Desktop/xlsToCsv/:/home/demo/Desktop/xlsToCsv/jars/*" xlsToCsv

Batch - Natives, Jars, and loose class files

I am running Windows 7 64-bit.
I would like to make a .bat file that will run my eclipse project's main.class using (preferably) only that .bat file. The project is still undergoing construction so exporting a runnable jar every few minutes isn't an acceptable solution (nor is installing eclipse on all machines). Furthermore I don't really want to install any more plug-ins, I know there is a way to do this with a .bat but haven't been able to figure it out. Here is what the project root folder looks like:
C:/.../ShootEmUp <-inside there we have:
bin/
lib/
natives/
lots of .jar files
res/
src/
ShootEm/
OtherPackages/
ShootEmMain.java
.classpath
.project
RunMe.bat
I have tried more combinations inside the .bat file than I care to admit. Right now RunMe.bat looks like:
#ECHO OFF
javac src.ShootEm.ShootEmMain.java
java -cp lib/*.jar;. src/ShootEm/ShootEmMain;
PAUSE
This currently produces a javac: file not found: src.ShootEm.ShootEmMain.java error
I have also tried putting the .bat inside the folder ShootEmUp/src/ShootEm/ (next to the class with public static void main [ShootEmMain.java]) and editing the RunMe.bat code to:
#ECHO OFF
javac ShootEmMain.java
java -cp ../../lib/*.jar;. ShootEmMain;
PAUSE
This seemed to work a little better as the error I would get then was:
ShootEmMain.java:5: error: package ... does not exist. (the a whole bunch more for all the other jars in the lib folder)
It's useful to note I've already added C:\Program Files\Java\jdk1.7.0_25\bin; to the front of the value of the Path environment variable.
I have also tried classpath instead of cp and a whole crazy list of syntax combinations for each (once I was well past frustrated) to no avail.
Also I realize my .bat example doesn't set the natives as I'm not really sure where that fits into this mess. Thank you in advance!
You should look at using a tool like ant or maven to build your application. This will streamline producing your artifact.
If it's unreasonable to export a working jar across your environment, I boggle at how you could imagine it's more manageable to export your source code around your environment.

How to use javap with eclipse?

As the title states, I'm trying to use javap with eclipse but have difficulties setting it up. I'm trying to set it up using external tools from the run menu but can't find the correct Arguments: string to make it work. Basically I need something that will dynamically execute the current file I have opened.
I use the following external tool configuration to achieve this:
${system_path:javap} is used to locate javap in the JDK used by the Eclipse. You can use an absolute path to javap instead.
${project_loc} returns the absolute path to the project. This is used, since I could not find a pre-define variable to use, to locate the .class file of a resource, and that's why javap runs in the project's directory instead of the directory containing the .class file.
Among the arguments passed to javap:
bin is the default output folder for Eclipse projects. Change this to build/classes or whatever is used by the project. Note, the value is relative to ${project_loc}; you can specify absolute paths instead.
${java_type_name} is used to obtain the selected class name.
You can select a Java file in the Project explorer view or Project navigator view, or even a Java type in any of the views, and then run the external tool. Note - this approach doesn't work quite well when you select a method, an inner class etc. and then run the tool, as they are not resources on their own, leading to the scenario where ${project_loc} will be empty.
Your problem is that javap requres path to class file but when you select your source file you can access eclipse variable ${selected_resource_loc} contains path to java source. As far as I understand there is no variable that contains path to class file.
I think that the easiest way for you is creating your custom script that accepts path to java file, replaces java to class and source folder to bin folder. If you are using linux it it can be easily done using command sed. If you are on windows it can be implemented using command SET with ~. See help for more details.
Good luck.
Please try by modify the Working Directory to match your java project output folder. In my case, it looks as given below.
Working Directory: ${workspace_loc:/Sample/bin}
Then I selected the .class file and executed the javap without any issues.
In order to disassemble the currently selected .class file in Eclipse I use the following arguments in the External Tools Configurations.
This way classes in subpackages of the bin folder can also be disassembled. The output is displayed in the Console view.
Oracle documentation of javap parameters.
In-addition to the valuable custom script suggested by #AlexR; the other way is : Open the terminal windows within the Ecplise and run the javap command with -p and other option.

How can I decompile many jars at once?

Well, I guess subject says it all :)
The ideal solution would find all jars within a certain folder (they might be in sub-folders), and write all the sources found into a single "src" directory, of course maintaing the package folders.
Concrete use case: decompile all Eclipse plugin jars
Download JAD Decompiler.
Unjar all your jar files (using the command jar xvf) to some directory. Let's call this ${unjar.dir}.
Create a directory for JAD to write out the decompiled sources. Let's call this ${target.dir}.
Execute the following command:
jad -o -r -sjava -d${target.dir} ${unjar.dir}/**/*.class
options are:
-o - overwrite output files without confirmation
-r - restore package directory structure
-s <ext> - output file extension (default: .jad)
http://java.decompiler.free.fr/
Decompile eclipse plugins??
Apart from the existence of a technical solution - (1) you can get all source files for the (official) eclipse plugins as bundles and (2) don't expect that the decompiler results will compile or can be used to debug the code.
So if you want to study the source of eclipse and it's plugins, decompiling is .. say .. not the best idea.
I wrote a tool names code-collection using shell script and cfr to find and decompile all jar, war files at once.
After decompiling jar, war files, you could use my tool to find and copy all specific files (.js, .html for example) to a new directory.
Visit my project at: code-collection
The Windows equivalent command for this would be:
jad -o -r -sjava -d "target.dir" "unjar.dir/**/*.class"

Categories