How to execute llvm-ir in java - java

I have written a compiler in java which can output llvm-ir. Now I would like to actually run the compiler somehow. I've read about GraalVM and how it should be possible to run other languages which use LLVM inside of java, however, I couldn't find out how to run llvm-ir itself with it. I don't have to use GraalVM though, any suggested approach would be fine as long as it enables me to somehow execute that code.

Basically, you need to get yourself a GraalVM, and you can execute your LLVM bitcode file with
lli [LLI options] [GraalVM options] [polyglot options] <bitcode file> [program args]
as per
https://www.graalvm.org/reference-manual/llvm/
The lli launcher lives in the bin folder of the graalvm distribution.
Regarding the format of the bitcode file the docs on https://www.graalvm.org/reference-manual/llvm/Compiling/ say
While the
GraalVM LLVM runtime can execute plain bitcode files, the preferred
format is a native executable with embedded bitcode. The executable
file formats differ on Linux and macOS. Linux by default uses ELF
files. The bitcode is stored in a section called .llvmbc. The macOS
platform uses Mach-O files. The bitcode is in the __bundle section of
the __LLVM segment.

Related

How do I make a code that will make Java code/file to an .exe file [duplicate]

I want to convert a .jar to an .exe for microsoft. Is there any program converter for this?
Also if there's one for Mac and Linux I would appreciate suggestions for those too.
Launch4j works on both Windows and Linux/Mac. But if you're running Linux/Mac, there is a way to embed your jar into a shell script that performs the autolaunch for you, so you have only one runnable file:
exestub.sh:
#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
JAVA_OPT=""
PROG_OPT=""
# Parse options to determine which ones are for Java and which ones are for the Program
while [ $# -gt 0 ] ; do
case $1 in
-Xm*) JAVA_OPT="$JAVA_OPT $1" ;;
-D*) JAVA_OPT="$JAVA_OPT $1" ;;
*) PROG_OPT="$PROG_OPT $1" ;;
esac
shift
done
exec java $JAVA_OPT -jar $MYSELF $PROG_OPT
Then you create your runnable file from your jar:
$ cat exestub.sh myrunnablejar.jar > myrunnable
$ chmod +x myrunnable
It works the same way launch4j works: because a jar has a zip format, which header is located at the end of the file. You can have any header you want (either binary executable or, like here, shell script) and run java -jar <myexe>, as <myexe> is a valid zip/jar file.
JSmooth .exe wrapper
JSmooth is a Java Executable Wrapper. It creates native Windows launchers (standard .exe) for your Java applications. It makes java deployment much smoother and user-friendly, as it is able to find any installed Java VM by itself. When no VM is available, the wrapper can automatically download and install a suitable JVM, or simply display a message or redirect the user to a website.
JSmooth provides a variety of wrappers for your java application, each of them having their own behavior: Choose your flavor!
Download: http://jsmooth.sourceforge.net/
JarToExe 1.8
Jar2Exe is a tool to convert jar files into exe files.
Following are the main features as describe on their website:
Can generate “Console”, “Windows GUI”, “Windows Service” three types of .exe files.
Generated .exe files can add program icons and version information.
Generated .exe files can encrypt and protect java programs, no temporary files will be generated when the program runs.
Generated .exe files provide system tray icon support.
Generated .exe files provide record system event log support.
Generated windows service .exe files are able to install/uninstall itself, and support service pause/continue.
New release of x64 version, can create 64 bits executives. (May 18,
2008)
Both wizard mode and command line mode supported. (May 18, 2008)
Download: http://www.brothersoft.com/jartoexe-75019.html
Executor
Package your Java application as a jar, and Executor will turn the jar into a Windows .exe file, indistinguishable from a native application. Simply double-clicking the .exe file will invoke the Java Runtime Environment and launch your application.
If your program is "publicly available non-commercial in nature" and has "a publicly available Web site that meets the basic quality standards", then you can try and get a free license of Excelsior. If its not then it's expensive, but still a viable option.
Program: https://www.excelsiorjet.com
As a side note: Here's a study of all existing Jar to EXE programs, which is a bit depressing - https://www.excelsior-usa.com/articles/java-to-exe.html
Despite this being against the general SO policy on these matters, this seems to be what the OP genuinely wants:
http://www.google.com/search?btnG=1&pws=0&q=java+executable+wrapper
If you'd like, you could also try creating the appropriate batch or script file containing the single line:
java -jar MyJar.jar
Or in many cases on windows just double clicking the executable jar.
I used Launch4J and it works flawlessy,
First, Go to your jdk and copy the bin and lib folder
Then create a folder for your app and make a folder called jre-(version),
Then paste these inside
Then open launch4j Put the exe file's path (Inside the app's folder)
Then go the jre option and inside bundled paths type jre-(version)
Then in minimun version type the version of the java you coded it in.
Then in the jre option put "Only use private jdk runtimes"
Then hit the gear icon. It will open a file chooser.
Choose your application's folder, done
Now open the exe file generated. It should work.
In case it does not, try the test run button (The green play one). If it says LinkageError in the console below, then in the jvm-options type --enable-preview. It should work.
I hope it helped.
For Windows, you can convert jar to exe using following ways:
Using Netbeans
https://erainnovator.com/convert-jar-to-exe-file/
Using Excelsior JET
https://youtu.be/iQSfUb8chjg
if you need to convert from .jar to .exe from java 14 you can used jpackage
jpackage is a command-line tool to create native installers and
packages for Java applications.
this article will help you

If javac was written in Java, why I can execute javac as if it is a none-java program? [duplicate]

This question already has an answer here:
Why Java compiler as distributed as executable and not as JVM bytecode?
(1 answer)
Closed 7 years ago.
Java program needs to be packaged to JAR file so it can be executed using java -jar command. So why don't I have to execute javac with java -jar javac command? How did Sun/Oracle make java program into executable binary file?
I know there are tools that can convert jar file to windows executable file. But I want my jars to be executable in Linux/OS X without the help of bash script.
---------- UPDATE
I found this link really helpful: https://github.com/maynooth/CS210/wiki/Convert-Java-Executable-to-Linux-Executable
If javac was written in Java, why I can execute javac as if it is a none-java program?
The answer to your question has already been given by Jon Skeet for the question Why Java compiler as distributed as executable and not as JVM bytecode?
Quoting his answer here below
javac.exe (on my installation, JDK 1.8 on Windows x64) is about 15K in size. This isn't the full compiler. The compiler itself really is written in Java, and javac.exe is just a launcher, effectively. This is true of many of the tools that come with Java - it would be a pain to have to run something like:
java -cp path/to/javac.jar java.tools.Javac -cp path/to/your/libraries Foo.java
A simple way to understand the whole thing is imagining JRE(Java runtime environment) acting like an intermediate layer between your program and the OS.
JRE accepts the bytecode and runs your java program.The javac (java compiler ) converts your java source code to platform neutral byte code(exceptions are there). I am not sure if java,javac,jre is written in java or not. But if they are,then they have to linked/loaded in a different way in different OS(platforms).
Now coming to how to run jar in windows and linux
Normally java code is converted to jar file. Then there will be two files(mostly along with jar file) to start the jar file , one for windows and one for linux
For example Apache tomcat has files (in same location)
startup.bat ==> to start program in windows.
startup.sh ==> to start program in linux.
Alternately you could convert jar to exe for windows.
For linux the link you specified is enough. The script is interpreted by command interpreter in linux and your jar file will be executed.
This link specifies different ways of executing a shell script in linux.
http://www.thegeekstuff.com/2010/07/execute-shell-script/
This link has code to run jar as bat
Run .jar from batch-file
To sum it up , your jar file can be platform independent but they way to start the jar file will differ for different platforms.

Is Java required to run some .NET based programs?

Introduction
One of the comments to this question is about using GnuWin's file command in order to check whether a program is using java or not. However GnuWin's file command indicates the following:
C:\Windows\system32>file "C:\Program Files\Eclipse Foundation\eclipse\eclipse.exe"
C:\Program Files\Eclipse Foundation\eclipse\eclipse.exe; PE32+ executable for MS
Windows (GUI) Mono/.Net assembly
C:\Windows\system32>
while java is required in order to run programs, e.g. Eclipse, ApacheDS, Apache Directory Studio and Tomcat.
The discussion regarding this question resulted in a suggestion to ask a question at StackOverflow regarding the relation between .NET based programs and Java processes.
Question
Why does GnuWin's File Command indicate that certain programs are .Net based, while these require Java in order to run?
Eclipse.exe is not a java program: it is actually a native win32 executable that serves simply to locate and launch the JVM with appropriate commandline parameters and the path to the JAR file that contains the actual Eclipse java executable.
See for example http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fmisc%2Flauncher.html for full details.

Executing Java application that makes a JNI call, with eclipse, fails with UnsatisfiedLinkError: Can't find dependent libraries

I have a Java program that calls a function in a JNI library. The JNI code statically loads another shared library.
When executing the Java application using Eclipse, I get an error java.lang.UnsatisfiedLinkError: ... Can't find dependent libraries
But, if i execute the same command in commandline, the program works fine. What am I doing wrong in Eclipse?
I made sure to go to Debug View -> Processs -> Process Properties to get the same command string and same working directory as Eclipse execution.
Here is a PD procedure that might help you identify the problem.
Add the following to your program to identify the differences in the arch and load paths between the two runtime environments. Investigate any differences in path/arch.
System.out.println(System.getProperty("java.library.path"));
System.out.println(System.getProperty("sun.arch.data.model"));
You can use the dumpbin.exe utility to identify the dependencies needed by the DLL that is being loaded.
Make sure the dependencies exist.
Example usage:
C:> dumpbin /imports your.dll
Dump of file your.dll
File Type: DLL
Section contains the following imports:
**KERNEL32.dll**
You can use the where.exe command to find the location of the dependencies.
Example usage:
C:>where KERNEL32.dll
C:\Windows\System32\kernel32.dll
If you see:
C:>where KERNEL32.dll
INFO: Could not find files for the given pattern(s)
Investigate why the dependent DLL is not on the path.
You can use the dumpbin.exe command to check 64bit vs 32bit.
Example:
C:>dumpbin /headers yourd.dll
Dump of file yourd.dll
PE signature found
File Type: DLL
FILE HEADER VALUES
14C machine (x86) <-- 32bit DLL
C:>dumpbin /headers yourd.dll
Dump of file yourd.dll
PE signature found
File Type: DLL
FILE HEADER VALUES
8664 machine (x64) <-- 64bit DLL
Investigate any 32bit vs 64bit mismatches between main/dependent. If your JVM is 32bit, you need to use 32bit DLLs. If your JVM is 64bit, you need to use 64bit DLLs. ( It is okay to run a 32bit JVM on a 64bit OS but the JNI DLLs must be 32bit ( DLLs match the JVM not the OS ).

Can you run a Java program in Linux using the name only, without the "java" command?

If I am writing HelloWorld, is there a way I can run the program from any directory by just typing HelloWorld? Sort of the same way once you set up Ant, you can just run Ant from any directory?
Just for some details, we are creating a CLI based toolkit for a customer, and just curious if we can compile it, install it, and just have them run it using the toolkit name.
You can always create a shell script, call it HelloWorld and make it run java with your JAR.
You'll then need to chmod the script to make it executable, and place it somewhere in your $PATH.
The script would like something like:
#!/bin/bash
cd /path/to/helloworld
java -jar HelloWorld.jar "$#"
or
#!/bin/bash
java -jar /path/to/helloworld/HelloWorld.jar "$#"
depending on your exact requirements.
Common solution for your problem is to create a separate launcher application, which is non-java application that runs your Java program. Launcher can be written in some compilable language such as C/C++ and compiled into native executable. Also it can be written in some interpreted language such as Unix shell, perl, python etc and made executable by adding #!/path/to/interpreter line at the beginning of launcher file and setting executable flag on it. Also there are several utilities that can generate launcher for your program such as launch4j or jsmooth.
On Linux (specifically), you could use the /proc filesystem (see proc(5) man page) and its binfmt_misc (actually the /proc/sys/fs/binfmt_misc/register pseudo-file and other pseudofiles under /proc/sys/fs/binfmt_misc/) to register java as the handler for .class or .jar files. Read the Documentation/binfmt_misc.txt file in the kernel source for gory details.
Then any executable .jar file would be interpreted by java (or jexec)
I'm not sure it is worth the effort. I find that wrapping your Java program in some shell script is much more easy (and more portable, because few Linux systems actually use binfmt_misc, and your customer may need some sysadmin skills to enable it).

Categories