I use ant for creating .jar files in Eclipse. Works great.
I have a .jar file I am working on that expects the code to be in a .jar file (it looks for .properties files in the same directory as the .jar file) -- the standard Eclipse "Run" and "Debug" menus execute the main() method of a specified Java class... but they do it from the directory containing the compiled class files, not a jar file. Is there a way to change this behavior so Eclipse runs code from the appropriate .jar file instead?
(My workaround right now is to run the .jar file externally, with it suspended waiting for a debugger, per Dave Ray's answer to one of my other questions.)
You could use remote debugging by running your jar like this
java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -jar yourJar.jar
And then connecting from your IDE to that port
Yes, you can create a custom "Run Configuration":
Ie, a "Java Application" one, with:
Classpath tab emptied from its default content (the .class directory) and with the jar added
Source tab with its default content (should reference the src directory of the project)
One such configuration can be run or debugged.
(Example of a custom configuration with jars as user entries)
I just found the following link, which describes the whole procedure in order to debug a Java jar remotely.
Debug Java applications remotely with Eclipse
The main parts are:
Target VM acts as debug server
java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address="8000" -jar
test.jar
Target VM acts as debug client
java -Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8000,suspend=y
-jar test.jar
Based on how you run the target vm, client or server, you have to configure Eclipse differently.
Eclipse configuration if you start the target vm as client
Eclipse configuration if you start the target vm as server
The article gives also a gently introduction into the topic.
I would try to make the code more robust, make the properties file location configurable, or just make it load it from the classpath. Then you can just directly add the properties file to the eclipse classpath. Problem Sovled!
Related
I've been wanting to reverse engineer this clients launcher in an effort to understand how the game was launching as a Java application despite it only having a PE32 executable alongside it.
The launching of the client goes as follows:
java -Xmx384M -Dfile.encoding="UTF-8" -cp TargetBinary.exe com.java.client.Client
Now I was curious what TargetBinary.exe actually was, as this was being ran on a *NIX system. Running file I observed this output.
TargetBinary.exe: PE32 executable (GUI) Intel 80386 (stripped to external PDB), for MS Windows
The second part is the com.java.client.Client after the TargetBinary.exe, this stands out as a Java pathing which Client is my target.
Here are my questions:
How can Java add the TargetBinary.exe to its classpath?
As a followup, are there any recommendations to decompile it to the point where I can observe Client and more so understand how it was all packed together?
Being a Portable Executable (PE) the libs, code, etc should all be there inside the TargetBinary.exe and somehow Java knows what to do with it?
Zip files are read from the back, so it's easy to put a zip (jar) file at the end of an executable file, and have it work as both.
See https://en.wikipedia.org/wiki/ZIP_(file_format)#Structure
Try running jar -tvf TargetBinary.exe to see the names of the classes and resources in the jar. If you make the file a dependency of a project in your IDE you can see decompiled code and navigate around the project, and run it and set breakpoints.
I have an existing Java project that compiles and runs properly through Eclipse. I have created the following .bat file to run the program sans Eclipse:
java -classpath jflashplayer.jar;bin TestProgram
The file is saved within the project folder, but not within the bin folder (located in same directory as bin). When I try to run the batch, I am met with a large number of runtime errors, the first being
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils
I'm not sure why I get this error when it compiles and runs properly via Eclipse. I have the commons-io jar files linked to the project within Eclipse as libraries, and the jar files are themselves located in the project file (same directory as the batch file and the bin folder).
Also, I'm not entirely sure what the -classpath jflashplayer.jar bit of the batch file is doing. I am using the jflashlayer.jar library (also linked to the project within Eclipse and in the same location as the other jar files), but I am not sure why it would appear in the batch file. I edited an existing batch file from a similar project that uses the jflashplayer.jar files, and it has worked previously to leave that part in.
When I write code in Java, I rarely require it to compile/run outside of the IDE, so I usually have troubles when it comes to this part. Perhaps there is a more robust and foolproof method to run the program outside of the IDE other than the batch file method.
The batch file method is fine, but you have to specify all the libraries you're using on the classpath, just like the jflashplayer.jar.
In this case, the error you're getting is because the Apache commons-io library is not specified on the classpath. Your command would have to look something like:
java -classpath jflashplayer.jar;commons-io.jar;<other jars ...>;bin TestProgram
Alternatively, you can create a runnable jar from Eclipse as described here. When you select a library handling strategy, choose the option Extract required libraries into generated JAR. This will make it so that all the library classes you're using are packaged into your application's jar file, and you can just execute it by invoking java -jar my_application.jar.
I'm trying to run my Java program using a batch file, I'm able to run it properly. However, when I run the batch file after inserting the code to read a properties file from the Java program, I'm getting the following error.
Can't find bundle for base name app1, locale en_US
Actually i have a conf folder under which I have this properties folder, then I came to know that I need to keep this conf folder in the class path. But I have actually added it as class folder using Eclipse. However, I'm getting the same error. Please let me know what exactly I need to do for running the Java program using batch file. Using Eclipse I'm able to run the properly.
Thanks,
Balaji.
From the comments your batch script:
#echo off
java -Djava.ext.dirs=lib -classpath ./bin com.myapp.app1.demoprogram
pause
Notice how the /conf/ directory is not listed on the classpath. The easiest way to get it there is to just add it to the -classpath argument being passed to the JVM. Something closer to:
#echo off
java -Djava.ext.dirs=lib -classpath ./bin;./conf com.myapp.app1.demoprogram
pause
This is assuming that /conf/ is in the same directory as bin. You might have to do some tweaking to get the setup to work for you application, but the root problem is that while you added the /conf/ folder to the project classpath in eclipse, you need to do the same thing for the batch script so the JVM can find it
I have created an executable java Swing .jar application. It works fine on Windows. The application hierarchy is :
application.jar
images(Folder) .......... Contains all images the application uses.
libraries(Folder) ....... Contains all external jar libraries the application uses.
bundles(Folder) ......... Contains all bundle files the application uses.
database(Folder) ........ Contains the database files the application uses.
All the above folders exist outside the jar file. Now i am trying to create a Mac executable file (.app) from "application.jar" to run it on Mac so i used the "Jar Bundler" as specified here but when i run the output application.app file nothing happens, nothing runs and i can't even debug it.
I think the main reason is that it can't see the external folders. So is it impossible to create a .app file if the application has external folders ?
And is there a way to debug the .app file to see what's going on ?
Nothing runs, and i can't even debug it.
Diagnostic output from the launch process may be obtained as follows:
$ export JAVA_LAUNCHER_VERBOSE
$ ./YourApplication.app/Contents/MacOS/JavaApplicationStub
There's a related example here.
Most likely the problem is your working directory.
When you run an executable JAR file by double-clicking it, the working directory is the parent directory of the JAR file.
By default, the working directory of an application bundle is its parent directory. If you package the external folders into the application bundle they will be located under $APP_PACKAGE/Contents/Resources.
So the assumption about the working directory that you make for an executable JAR file does not hold for an application bundle.
In order to set the working directory to the resources directory, add
<key>WorkingDirectory</key>
<string>$APP_PACKAGE/Contents/Resources</string>
to the Info.plist file of your bundle.
In case you know nothing about application bundles, please read this document.
This might help: AppBundler by Josh Marinacci
I am not sure about your exact directory hierarchy. But on a Mac with Xcode installed is an application called "Jar Bundler". It exist for exact that purpose you are asking for.
BTW, Mac application use the suffix .app, that is right. But they are not files. Thery are directories.
Hi I have created a java application and created a jar file. Then I created a batch file eg-> startup.bat The contents of this file is java -jar MyApp.jar
Now if I double click the batch file then my java application works because both the jar file and batch file belong to same directory. Now I copied the batch file to desktop and run then My application did not run.
Also I want My batch file automatically set the class path.
How to create a batch file with all the above feature
Thanks
Sunil Kumar Sahoo
If the jar is an executable JAR (which it seems to be from your example), you should be able to just double-click the JAR file. If this does not work, you shoud associate the java executable as the program with which to open a file with the .jar extension. See Tools > Folder Options > File Types from an explorer window.
In order that a program be "distributable", it would normally be packaged (i.e. the JAR file and startup scripts) in some ZIP archive and then extracted to a folder on a user's PC. Your startup script should provide a relative path to the JAR file. For example if you have organized the contents of your ZIP into lib and bin folders:
MyProject
+--- lib (contains jars)
+--- bin (contains .bat script)
In this case, the startup.bat file should reference the Jar as:
%JAVA_HOME\bin\java -jar ..\lib\myapp.jar
This requires that any users:
Have a JRE installed on their system (unless you package that up as well)
Have java on their path or a JAVA_HOME environment variable set
The problem is that the batch file doesn't know where the jar is, it is looking relatively from its own path. You could copy the jar file to your desktop, too.
If just want an icon on your desktop, make a shortcut to the batch file rather than a copy.
Aslo if you application is quite complex (size more than 1+GByte) - you can distribute it with jre included.
Because it's a bit long to copy in here, I'd suggest that you have a look at what Squirrel SQL does. And you'll have a very nice cross-db tool as an extra bonus ;-)
They enable the tool to be started in two possible ways:
by starting the jar itself, classpath info will then be fetched from the manifest file (see here for the details, look up download extensions). In that case, after unzipping the hierarchy of files as explained by other posters, just make a shortcut on the desktop and you're all set
with a batch file. You'll see that the batchfile solution is quite complicated, especially the construction of the classpath.
So I'd go with the first solution.
I understand that you would like to distribute your application to multiple machines? A (very) simple solution would be to create a zip/tar/whatever archive that has all the needed jars (including MyApp.jar) and the startup script.
In the script you can use the -cp option to set the classpath to the jars:
java -cp needed.jar:needed2.jar -jar MyApp.jar
As said above, making a shortcut to the desktop is a better option than copying the script itself.