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.
Related
This question already has answers here:
Connect Java to a MySQL database
(14 answers)
What is a classpath and how do I set it?
(10 answers)
Visual Studio Code, Java Extension, how to add a JAR to classpath?
(10 answers)
Closed 10 months ago.
I'm working on a lab for a class where the teacher just said
Go here, install this, do the lab. "https://dev.mysql.com/downloads/connector/j/"
Now I followed that, installed mysql, ran a server, tested it with workbench, and a few queries. Works.
However the rest of the lab was in java, so I built a folder in VSCode, lab x, ran javac *.java, java Program
The error I got was "Client exception: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"
Now this makes no sense since I installed mysql with the JConnector, and added it to my computer's environmental variables, I looked online and all I'm finding is stuff about Maven and Netbeans, I'm not familiar with either. I just want to complete this lab.
I tried adding it to the folder, then saying "javac -cp mysql-connector-java-8.0.28.jar Program.java" but then any java file referenced within the same folder was a symbol it couldn't find.
BAD ANSWER - Old
I figured it out.
I opened the jar file in 7zip, dragged out the com folder and put it in my lab folder.
Works!!
GOOD ANSWER - VSCode - New
Download Java Extension for VSCode.
Go to Command Pallet (ctrl+shift+P.) and write Create Java
Project.
Click on Build Tools
Name it whatever your project's name is.
Drag/Drop your Java files into the folder "src" in the project's folder.
Look at the bottom left of your screen where it says Java Projects and expand it.
Hover on the right side of Referenced Libraries and click the + button.
In the popped up Open window find where you have Connector/J and select the .jar file (in this case, mysql-connector-java-8.0.28.jar) and click Select Jar
Libraries
Click the Refresh button on your Java Project and click Build Workspace and pick Full
Restart VSCode
Click the Run and Debug button / Press ctrl+shift+D
Works!
GOOD ANSWER - Command Line - New
javac -classpath "[Project folder or just a "." if your console is in it];[folder with jarfile]\\[jarfile].jar" Program.java
java -classpath "[Project folder or just a "." if your console is in it] ; [folder with jarfile]\\[jarfile].jar" Program
In this specific case
javac -classpath ".;C:\\Program Files\\MySQL\\Connector J 8.0\\mysql-connector-java-8.0.28.jar" Program.java
java -classpath ".;C:\\Program Files\\MySQL\\Connector J 8.0\\mysql-connector-java-8.0.28.jar" Program
So in the background the VSCode Answer does what the Command Line Answer does.
Basically every time you run java or javac, java adds a thing implicitly pointing/referencing where your classes (compiled .java programs) are (in a folder, or a zipped folder called a .jar), to your java/javac commands called a classpath in between the java/javac and your .java file.
With the implicit
javac Program.java
java Program
Being equivalent to
javac -classpath "." Program.java
java -classpath "." Program
The implicity makes sense as you would be using the classes from the folder that the program is in. (Note: The "." means the folder the Commandline is in, navigated by cd/ChangeDirectory in Bash/PowerShell, see Linux/Mac/Unix Path Formats and/or Windows Path Formats ).
Now you might be wondering why there's a ";", this is to denote multiple classpaths, as you want to look into your projects, ".", and the library JDBC, if you wanted to add more classpaths add another ";", however on Linux/Mac/Unix you would add ":" instead of ";".
The reason the old Bad Answer worked was because the dragged folder was included in the implicit classpath.
The reason the old Bad Answer is a bad answer is because its reflective of poor practices and lack of understanding. Its poor practice for few reasons: the zipped files take up less space thus unzipping needlessly eats space, these files aren't yours so mixing them with yours could violate the terms of use of the author, and its a lot of work unzipping that you're doing for no reason.
I've been trying to get this to work practically all day. I'm fairly new to programming and I had an assignment for school to create a chat server (though this is beside the point). So I finished my program and wanted to test it but no matter what i do my class files cant be found in the command prompt for some odd reason. Ive tried everything as far as changing the path and I'm not sure whats going on. I did a quick hello world java file just to make sure it wasn't something in the actual program but I ran into the same problem. I screenshotted the error i keep getting and attached it. As you can see, the helloworld is in the right place on the desktop but for some reason it just wont find my files. Thanks for nay help you can give.
Screenshot of Error
Just check whether you have saved your file in .java format.
To check that, right click your file and click "properties" option to view your file format. I have added the screenshot of my java file. If you look into that, Type of file is "JAVA file .java"Attached screenshot
As Sneha Veerakumar's answer, make sure your file is in .java extension. Not .txt or other. But if the file has been in .java and the same issue still appears, check your JAVA_HOME environtment variables, make sure this is set correctly as well.
JAVA_HOME environtment variables in Windows:
Go to Control Panel > System
Navigate to Advanced system settings
Choose Environment Variables.
Edit/Add JAVA_HOME variable with the value of the JDK path, example: C:\Program Files\Java\jdk1.8.0_172
Add the JDK bin path C:\Program Files\Java\jdk1.8.0_172\bin to the value of 'Path' variable. With semicolon(;) delimiter.
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.
I have researched several examples similar to this issue but i have yet to find one that is the solution to my problem. I am simply trying to do my first program using Native methods. I don't have the program stored in a hiearchy of packages because i tried to keep it as simple as possible for my first example. Here is how everything is stored:
I Have one class called NativeDemo. It is stored in C:/JavaFiles/demo/. I compiled the program and the .class file is stored in this same folder. When I try to invoke the javah command on this program it tells me the class file can't be found. Can you please tell me what I am doing wrong?
My javah command is stored in my JDK and is stored as follows: C:/jdk1.0.7_04/bin/
On the command line from the command prompt I type:
C:\JavaFiles\demo>C:\jdk1.7.0_04\bin\javah -jni NativeDemo
also tried C:\JavaFiles\demo>C:\jdk1.7.0_04\bin\javah.exe -jni NativeDemo
When I do this it tells me the class file can't be found. This is the same path used when compiled and it found the file, compiled, and created the .class file with no issues.
Please help. Thanks.
You probably need to supply a -classpath parameter on the command-line to javah to set the classpath. (The -classpath parameter for javah behaves the same as it does for other Java tools; e.g. java, javac, javap and so on. If you don't understand classpaths, read this page and this page.)
As the solution was never added I thought I'd contribute the solution I found to this problem. It wasn't really a solution as such as it was 100% user error!.
In what sounds like a similar situation I wanted to create a quick small project to demonstrate the use of JNI. As this was designed to be a simple exercise I didn't bother with an IDE and simply used vi to write the code and javac to compile it.
e.g
myclass.java (fully qualified class name is mypackage.myclass)
javac myclass.java
The above command outputs myclass.class to the current directory. I now have myclass.java & myclass.class in my current working directory.
Running javah mypackage.myclass results in the error described.
The problem here is my use of javac, I wrote the compiled class to the current directory, javah however is looking for it at "/mypackage/myclass.class".
Silly little problem with a silly little answer but I was quite annoyed at wasting 15 minutes on this today so hopefully I can save someone else the same pain (and yes I know I should have spotted it sooner and may have had I not just had an entertaining few hours finding System.load discrepancies between hotspot and gnuj java implementations, so sadly I wasn't exactly trusting my tools :( !! ).
You have to use fully qualified name for the class.
The syntax for javah is javah [options] classes. classes are specified with their fully qualified names. So, in your case if ur package is demo then, the command would be,
C:\JavaFiles\demo>C:\jdk1.7.0_04\bin\javah -classpath . demo.NativeDemo
Classpath is . because, as you mentioned you have .class file in the current directory. And -jni option is not needed, cuz its default.
in android studio 1.5 I had same problem and run above commands but nothing , I explored the app module build folder and there wasn't classes folder in intermediates folder so before run commands make the project, you can do this by Build>Make Project or shortcut key Ctrl+F9, then run above commands
I solved this problem with the following command used in the directory of the eclipse project:
javah -classpath [PROJECT_DIR\bin\classes] class.name
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.