Java Compiling File Directory Issues - java

I'm just a guy trying to learn some Java. The actual coding part works just fine for me, and until now I've been working in Eclipse (to new guys like myself, Eclipse is a really great free piece of software for writing and compiling code). But Eclipse compiles the code by itself, and I'm determined to learn how to do this without any training wheels, seeing as I'll probably need to do so someday. However, I already tried manually compiling my code with Command Prompt a few months ago, and ultimately I just got tired of banging my head against the brick wall. It seems that when I try to compile a simple HelloWorld file in cmd, everything that can go wrong, does go wrong. And it seems that I'm not the only one who keeps getting thrown off code because of this ridiculous first hurdle. Until now I've slowly been able to work around everything that keeps popping up, but this new problem is completely beyond any advice that I can find on the Internet.
I have my HelloWorld class ready to run. It is in the same file as my .java file. I have managed to compile it with the use of an environment variable specifying the location of the javac tool. Now all I need to do is invoke the java tool to run the file.
Creating a new environment variable specifying the location of java gives the following error message on cmd:
"Error: could not open 'C:\lib\amd64\jvm.cfg'"
I have no idea why it's picking this directory, leaping straight to \lib instead of going through \Program Files.
Specifying the location of the java tool in cmd, as my book tells me to do, gives the following error message:
"'C:\Program' is not recognized as an external command, operable program, or batch file."
which of course is only the case because I'm trying to specify the path leading to the java tool, whilst my command directory has already been confined to 'C:\Java Code', the location of my class file, which is necessary because that's the file I'm trying to run.
So in short, the only way around this I can see is to specify two command directories at once, which I'm pretty sure is impossible.
Solution
In my case, this was solved by two things; first re-installing Java. It seems that I was using the wrong architecture version (Oracle's site may have been assuming that my computer was 32 bit). This seemed to deal with the 'cannot find jvm.cfg issue'. Second, configuring the system variables as described in one of the responses and on this page

Right, I had exactly the same problem with compiling my code for the first time. jvm.cfg is always a difficult file. The way i fixed it was: To edit the PATH variable or JAVA_HOME to the path to jvm.cfg. jvm.cfg is a configuration file for java.

Related

Class Names Are Only Accepted if Annotation Processing Is Explicitly Requested Java

So I found a Tower Defense game on Github that I'm planning to use so I can see how this person made the game and use that info to make my own. In their readme file, they say
INSTALLING/STARTING THE GAME:
command line instruction:
javac BattleMap
java BattleMap
The first line compiles all the source code. The second line launches the java application.
So in Command prompt I type:
javac BattleMap
and it doesnt work. It says, of course, "class names are only accepted if annotation processing is explicitly requested"
So every other topic I've found on this seems to suggest adding .java at the end, and that magically fixes it. Then people also say, even though the first answer worked, to make sure you're typing .java and not .Java
So of course I tried typing
javac BattleMap.java
and it still says "Class Names Are Only Accepted if Annotation Processing Is Explicitly Requested Java". I even tried .Java and instead of javac just typing java BattleMap and all the combinations of everything I could find, and I cannot for the life of me figure out how to open this stupid little file that I downloaded off the internet. I'm running Windows 7, CMD is being Run as Administrator, and it doesn't work. Can anyone help? More importantly, can anyone help without marking this as a duplicate? Because that would be great.
I downloaded the Zip from Git opened the command window in folder where the BattleMap class is and typed in command window
javac Xlint- BattleMap.java
next it shows some warnings and then I typed
java BattleMap
The game runs properly for me.

Java Exception has occurred in .jar

I made a .jar file using NetBeans. When I move it to any place on my laptop and double click it, it will work. When I try it on a different PC, it gives me a message box saying:
title: Java Virtual Machine Launcher.
text: A Java Exception has occurred.
Every PC i tried so far, gives me this. Those PCs do not have any additional Java software installed, only the basic one which updates itself automatically. If there is a way to share my Java file on different PCs, please tell me (even if this involves converting the file to some other format, but not that the user has to install anything extra).
Here is my error:
If in the PC where the program was created works, but in any other one fails, it seems it's a problem with some resource. Maybe you used an absolute path to find a resource file but that file is not in the jar. I think it works because the file can be found in your PC, but not in anyone else.
How about creating a bat file and setting java home inside it. So you could first set the home or path and then use java jar commands to execute the jar.

How to get my program to run outside of eclipse

I have a project in eclipse. It does everything I want it to do when I click on the green circle play button in the IDE - opens the window, plays the stuff, everything. But, try as I might, I cannot figure out how to get it to do that outside of eclipse.
My project uses the processing.core library to do some of its stuff, but I am unable to get the project to function in the Processing IDE, because of some stupid stuff about one of my classes not being a valid substitute for the type parameter for Collections.sort(List<T>). If anyone knows how I can get it to export from the Processing IDE, that would be excellent.
I need to be able to email/upload to the internet/otherwise transmit some kind of file/folder/webpage that allows the recipient to, without much technical knowledge or work on their part, view a window or something that allows them to view and interact with the program in the same way I am able to interact with the window that Eclipse launches when I press the play button at the top.
I have tried several different ways of accomplishing this, but none of them have worked. I tried exporting a runnable .jar, but it wouldn't let me include the referenced libraries. I tried a regular .jar, but I don't know how to package that up with whatever is needed to be able to view it. I've even tried using the fat jar eclipse plugin for it, too, but I ran afoul of something about being unable to find the main PApplet class for the project.
If anybody knows how to get what I want to happen, or knows of a good tutorial on how to do what I want, I would greatly appreciate any sort of assistance or guidance or anything.
Google has been unhelpful in turning up solutions to this problem, because most of the results I have found were just other people asking the same or a similar question, and then either no answer, or something I had already tried, with no indication of exactly what sort of options or settings I needed to give it in order to accomplish the task.
I am using the processing.core.jar from version 2.0b7, but I don't think that would make a difference, although I plan to try it with different versions if I can't figure out anything else.
A copy of my project folder can be found at https://www.dropbox.com/sh/1n4curhxbgi8fye/A5F6_l7xQu
All the data I have is stuff I've concatenated together from successive versions of the file at http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt
I've done a quick test on OSX and had to tweak your eclipse project a bit:
You are linking to .jar libraries using absolute path. I recommend using keeping libraries relative to the project
I ran into some memory issues and had to add a couple of compiler flags
Added a main method in Earthquaker.java to initialize as an application.
main looks like this:
public static void main(String[] args) {
PApplet.main(Earthquaker.class.getSimpleName());
}
In eclipse you can export a runnable jar via File > Export > Java > Runnable JAR file. Here are a couple of screenshots:
Personally I prefer using the "Copy required libraries into a sub-folder..." option in case I need to update some dependent .jar independent of the main application .jar.
For reference I've uploaded the modified eclipse project here(Java SDK will need to be changed). The exported runnable jar with a bash script is available here.
And here is the bash script itself which should run on Linux as well:
java -Xms128M -Xmx1024M -jar Processing-DataVisualization.jar
Pretty cool project btw.
Create a simple jar. Create a batch file and specify the class path. Save the batch file. Runnin the batch file will run the app
#ECHO OFF
set CLASSPATH=%CLASSPATH%;myjar1.jar;myjar2.jar;
java mypackage.MyClass

noClassdeffounderror with external jar when running code from a different location

I'm learning Java and was trying to test my code on another machine but am running to the above error. I've looked at youtube videos, read forums(and SO) but still can't get this to work.
I basically wrote some code on my mac using eclipse that referenced an external jar file. I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path". The code works fine on my laptop.
But when I try to run it from a linux command line, I get the above error. I am taking the code from eclipse and copying it to a file(first_try.java) and then run this commands:
CLASSPATH=./jedis-2.0.0.jar;export CLASSPATH
javac first_try.java
java -classpath . first_try
but then I get the error: Exception in thread "main" java.lang.NoClassDefFoundError: redis/clients/jedis/Jedis I downloaded the program via wget and checked permissions and tried different variations. There was a similar question I found(out of the many related ones) that had a similar context as mine but it worked for the user to just type java filename (which is not working for me)
I am learning a bit of java code but have never been successful at running external jars. I'm wondering what I'm doing wrong and what I can do to permanently fix it(ideally I want to write code locally and then copy it and test it on another machine like this)?
Or is there a better way to deploy code that depends on third party jars to other systems?
You are building your class path env var but then you don't use the value in your java command. You need $CLASSPATH instead of the dot after -classpath
Good luck learning java, I have enjoyed using it for over 10 years now ;)

Why does my JAR file execute at CMD, but not on double-click?

So I've been writing a simple 3D GUI application that I intended for users to use simply by double-clicking on the JAR file. I got it working perfectly before putting it into the JAR file, and I got it working perfectly IN the JAR file while running from command prompt (typing "java -jar Modeler.jar" while in the directory of the jar file). However, when I double-click it, nothing happens. It runs perfectly fine with no errors from command prompt. I know from experience that crash reports on start-up are not shown because the console doesn't appear (or it disappears too fast), but when running from the command prompt there are no crash reports. Any ideas as to why it won't work? I'm running Windows 7 Home Premium. Here are the contents of the JAR file if it helps:
Modeler.jar
|
+--*all the class files necessary*
|
+--META-INF
|
+--MANIFEST.MF
Contents of MANIFEST.MF:
Manifest-Version: 1.0
Built-By: AnonymousJohn
Class-Path: bin/j3dcore.jar bin/j3dutils.jar bin/vecmath.jar
Created-By: 1.6.0_16 (Sun Microsystems Inc.)
Main-Class: Start
EDIT: So after messing with the file associations to use java.exe instead of javaw.exe (thereby providing a window for print-outs), then modifying the startup mechanism a little to print out the current working directory, I discovered that the jar is running from "C:\Windows\system32" instead of the folder on my desktop I put it in. Go figure. However, moving the necessary outside files there doesn't help anything.
EDIT 2: I tried making another JAR file, this time with a simple JFrame with a button in it that tells you the current working directory. Press the button and it opens a (useless) JFileChooser. This worked on double-click no matter where I put it in my computer. So there must be something wrong with my JAR file. I'll start troubleshooting my program again.
EDIT 3: The problem is just what I thought it was: it's not loading libraries correctly when I double click on it. The weird part is that in my tests where I display the current path and library path, the output is exactly the same whether I run it via command prompt or via double-clicking on it. Here's the stack trace:
java.lang.UnsatisfiedLinkError: no j3dcore-d3d in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at javax.media.j3d.NativePipeline$1.run(NativePipeline.java:231)
at java.security.AccessController.doPrivileged(Native Method)
at javax.media.j3d.NativePipeline.loadLibrary(NativePipeline.java:200)
at javax.media.j3d.NativePipeline.loadLibraries(NativePipeline.java:157)
at javax.media.j3d.MasterControl.loadLibraries(MasterControl.java:987)
at javax.media.j3d.VirtualUniverse<clinit>(VirtualUniverse.java:299)
at javax.media.j3d.Canvas3D.<clinit>(Canvas3D.java:3881)
at ModelPreview.<init>(ModelPreview.java:51)
at Modeler.<init>(Modeler.java:76)
at Modeler.main(Modeler.java:1227)
at Start.main(Start.java:92)
Only problem is that it IS in the library path. I specifically set it in the program. Now that I think about it that may be the problem. I set it like so (this was a method I found somewhere on the internet. I don't remember where):
//above was code to get newPath based on the Operating System.
//all this code is set in a try-catch phrase.
//reset the library path
System.setProperty("java.library.path", ".\\bin\\natives" + newPath + ";");
//make sure the ClassLoader rereads the NEW path.
Field f = ClassLoader.class.getDeclaredField("sys_paths");
f.setAccessible( true );
f.set(null, null); //ClassLoader will automatically reread the path when it sees that it is null.
EDIT FINAL: Well, after looking and relooking at my code, I discovered the problem was in some BS'ery involving the detection of 64-bit systems where it was loading the wrong dll's. Why it worked from the command-line and not via double-click I don't know and will probably never know, but it works via double-click now, so I'm happy. Sorry about the troubles.
Ok, so I was stuck on this exact issue for over a week now (it has been for a side project for which I could only devote a few hours a day).
This happened on my desktop, but for some reason wouldn't happen on the laptop.
After looking around, I found this answer and I thought I'd share it for people who are like me, found nothing useful in the accepted answer here. Credits go to anonymous Stack Overflow user, whom username I've lost in all the excitement.
As mentioned by some other answer somewhere to an seeming unrelated issue, use this little program to associate your JAR files to the 64 bit version of java:
http://johann.loefflmann.net/en/software/jarfix/index.html
Save the program somewhere and run it from command line with the parameter /64:
c://path//jarfix.exe /64
Nothing else worked for me, but this was like magic. :)
The JAR is executable from CMD. That means the JAR itself is formed correctly. Good.
The only reason to fail now is that double-click produces not a right command. Expected command, as you correctly said, is
java -jar Modeler.jar
But when you associate javaw.exe with a JAR extension, I suspect it executes
javaw Modeler.jar
It is easy to check: make a javajar.cmd file, containing the following
javaw -jar %*
and associate it with JAR. If you app starts OK, I'm right. Otherwise, sorry.
I got fed up with not being able to modify file associations in Windows 7 Control Panel and edited the registry (NOTE: it's not a bad idea at all to set a restore point before proceeding if you think there's ANY chance you may screw up)(I neither set restore point nor screwed up):
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\jar_auto_file]
#=""
"EditFlags"=hex:00,00,00,00
[HKEY_CLASSES_ROOT\jar_auto_file\shell]
[HKEY_CLASSES_ROOT\jar_auto_file\shell\open]
[HKEY_CLASSES_ROOT\jar_auto_file\shell\open\command]
#="\"C:\\Program Files\\Java\\jdk1.7.0_60\\bin\\javaw.exe\" -jar \"%1\" %*"
This assumes that .jar files have (Default) value jar_auto_file. If not do this:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.jar]
#="jar_auto_file"
p.s. One problem I found in the registry was that jarfile was associated with Netbeans, Chrome, and java.exe as well as with Applications\java.exe. I deleted that entire node (after exporting it, just so I could put it back if it was essential; it wasn't). Now there is NO jarfile in my registry and all .jar files execute as before.
This manifests itself by showing more than one file type associated with .jar files in Control Panel. You want this:
If there's more than one item shown in Recommended programs, search registry for jarfile and (export first, then) delete that node:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.jar
Prerequisite: make sure JRE(that is included in JDK) is installed.
Solution
Open regedit and navigate to Computer\HKEY_CLASSES_ROOT\jar_auto_file\shell\open\command
Add "-jar" to value data. For example, change from "C:\Program Files\Java\jdk-15.0.1\bin\java.exe" "%1" to "C:\Program Files\Java\jdk-15.0.1\bin\java.exe" "-jar" "%1"
Done
If problem still occurs, make sure you configure correct main class in project property.
Just to put my two cents in, I had this problem and it was caused by something-or-other in the manifest, I suppose. I resolved it by generating the executable JAR in a different manner:
Previous developer generated the Runnable JAR from Eclipse (right-click project -> Export... -> Runnable JAR) and this worked fine for everyone. Then I came along and generated the runnable JAR by configuring a Maven plugin ("assembly") in the pom.xml and so a classic Maven build would produce the runnable JAR.
This worked nicely for me (from cmd and by double-clicking) but not for the end-users (who could still run the tool from cmd, but not by double-clicking the JAR).
I generated the runnable JAR from Eclipse and now everyone is happy again. Not sure why the maven-generated JAR wasn't ok to run by double-clicking, but I can't be bothered to investigate further now that the job is done. Hope this half-advice helps someone.
I had same issue with .jar files after trying everything i realized that I have more than one versions of Java installed. Removing the unnecessary versions solved my issue.
May be because the .jar was confused between different versions of JRE.
Just so that the answer to the question is clear to anyone passing by, I'll put my solution here (I couldn't before because of the 8-hour rule):
Well, after looking and relooking at
my code, I discovered the problem was
in some BS'ery involving the detection
of 64-bit systems where it was loading
the wrong dll's. Why it worked from
the command-line and not via
double-click I don't know and will
probably never know, but it works via
double-click now, so I'm happy. Sorry
about the troubles.

Categories

Resources