I have a .java and .class file which i put under a folder inside my pydev project in Eclipse (because im primarily using python).
Inside my python script i wanted to call the java class file using os.system.
os.system('java -mx1500m D:\\projects\\socialsense\\src\\ss\\samplefile\\test')
However it says that my class file is not found. What is wrong?
I expect that you can do that. However, most people would probably put the java source and python source in separate directory trees, and also not put .class and .java files in the same tree. (If you lump everything into the same place you'll cause yourself pain when you try to implement a "clean" rule in your build file ... or when you want to check your project into version control.)
Your immediate problem is that you've got the command syntax for the java command wrong. The name of the entry point class is specified by giving a fully qualified class name, not a pathname. And you probably need to use the -cp option as well.
For details on how to do this right, refer to the java manual page.
Related
I'm trying to use VS Code with java, but I have an issue.
As a student, my teacher gives me a file named Clavier.class.
I have already wrote a programm with another IDE, and when I execute it in the command of Windows, it works without any problem. But, when I put this programm in VS Code, it doesn't recognize Clavier.saisirInt, which is a static method in Clavier.class. I guess that VS Code can't use a class which is in a File.class, but is there a way to make it works ?
Here is an image of my workspace and you can see the error at the bottom
I assume your lib/ folder is placed onto the execution classpath. If this is the case, that's where compiled class files should go, not next to your sources
Otherwise, you'll need to inspect the java command that actually runs your code to see what/if the -cp argument is set to, or include those class files similar to how the documentation shows for JAR files (which are just zipped packages of class files, and honestly what the teacher should provide instead)
By default, VS Code will reference all JAR files in workspace's lib directory using the glob pattern lib/**/*.jar
And so, you need to adjust this to include lib/*.class after moving your files there
When compiling java code the I have been told the compiler must be run from the top of the package.
That is if I am trying to compile Test.java which is in tools.testing I have to first set the top of the package hierarchy, the folder containing /tools in order for it to work.
The class I am trying to compile uses another class contained in the same package and as such passing the full path of the code to the compiler prevents it from seeing the other class (as it doesn't search current directory and instead searches for the package inside of itself: ./tools/testing when it is already in /tools/testing )
I wanted to know if this was always the case or if there was a way to, for example: provide the path to the top of the package (since passing full path will not work for me) as an argument of the javac command or something similar ?
Thanks !
You should use an Integrated Development Environment (IDE) like IntelliJ, Eclipse or Netbeans. In an IDE you can create a Java project which has a directory acting as the 'source root'.
If you use Maven as your build tool the default location for such a directory is /src/main/java/ (this is the de-facto standard for Java projects at this time).
The IDE will automatically compile your Java files for you and allow you to run them easily during development.
If you want to run the application stand-alone you have to package it in some way. One simple and effective way is to generate a .jar file which contains all the .class files and other files you need (like images, .properties files etc). If you specify a pom.xml file for your project (that's Maven again) and set the packaging to jar Maven will automatically create a .jar file for you. You can even make the .jar file runnable with some additional settings.
See also this answer for some more info about packaging.
I'm trying to use a .class file I downloaded for a uni project. I don't have the .java file to go with it. I am using netbeans.
I tried just adding it to the project src folder
I tried using "add JAR/folder" on libraries and adding the directory containing it
I also tried creating a JAR file of the directory containing it and adding that
Greatly appreciate any suggestions
You should do two things:
Create a jar of the class file you received
Create an overview which methods the classfile offers
For the latter you have at least two options. One is to use a decompiler (some authors of APIs deny you to use this) like JD-GUI. The second options is to use javap, which comes with your JDK (I link to Java 8, but it exists in prior versions too). Simply call javap yourfile.class and you will see which method signatures the class offers.
But the easiest way to see the classes / methods inside the .class file is JD-GUI, so if you are not running into any legal issues use that approach.
I have a client/server program that attempts to send and receive an object.
There are three packages: server, client and shared
shared contains only the Message class
I put Message.java from shared package into the same folder as calcclient package source files and calcserver package source files.
I compile using the line: javac -classpath .; (long list of client or server.java files) Message.java
They can compile.
Then I change directory up one level and ran with: java -classpath .; .Main
When I use Netbeans to run, the entire program works as per normal. But not if I run from command line. If its executed through command line, the program will work until it needs to use the Message object. Then it will show a NoClassDefFoundError
Am I putting the right files at the right places? How do I get the program to find shared package through command line?
The files are not in the right place. The Message class belongs to a different package so it shouldn't be living with the other classes. From http://java.sun.com/j2se/1.5.0/docs/tooldocs/findingclasses.html :
User classes are classes which build
on the Java platform. To find user
classes, the launcher refers to the
user class path -- a list of
directories, JAR archives, and ZIP
archives which contain class files.
A class file has a subpath name that
reflects the class's fully-qualified
name. For example, if the class
com.mypackage.MyClass is stored under
/myclasses, then /myclasses must be in
the user class path and the full path
to the class file must be
/myclasses/com/mypackage/MyClass.class.
If the class is stored in an archive
named myclasses.jar, then
myclasses.jar must be in the user
class path, and the class file must be
stored in the archive as
com/mypackage/MyClass.class.
You have a couple of options:
The best solution is to take the time to learn Ant. Netbeans projects are built with Ant, which is a really great feature of Netbeans in my book, and you can open up the build.xml in your project and find a reasonably well commented description of what Netbeans does to build your project. And really I don't think there would be many places around that run builds from the command line so learning something like Ant would be a great help.
The next level down in sophistication would be to manually build a Jar for your shared package and put it somewhere on the classpath.
The most basic approach is just to compile the java files into class files and put them in the appropriate directory reflecting the package name as explained in the quote above.
If you build your project in NetBeans, you'll see that there is a dist folder where you can find your project in binary code. After building the source code, NetBeans specifies how should you start your project from command line.
If you use this and the problem persists, you should rebuild your Message class as a library, link it to the project using NetBeans and the project should work from command line using the command specified in NetBeans.
If you want to manually compile your source files, I think the best solution is to google something like this:
manually compile Java source code
Quote the classpath value if it contains spaces.
java -classpath C:\Program Files\java\...;C:\...
^
This is what's killing you slowly.
Try it this way:
java -classpath "C:\Program Files\java\...;C:\..."
If there's one thing that annoys me about Java it's that you can't double-click a class file so as to run. I assuming there's an entry in the registry that has to be edited to do this but I haven't a clue.
So, as it says on the tin. Does anyone know how to associate Java class files to run on double-click on Windows (I aiming for Windows 7 here but I'm sure there'd be no difference in the three most latest releases)? It would make my life (and I'm sure many other people's) much easier!
Udpate: I've seen answers relating to making a JAR out of the class in question and running it that way. However useful, that is not exactly what I'm looking for here. I'm effectively looking for Windows itself to invoke java with the class on double-click, with the correct arguments.
if classpath doesnt matter too much, easily done with a simple batch file runjava.bat or so that is associated with .class files in the explorer (via right click >> open with..)
#echo off
REM change to folder where the class file resides
cd %~d1%~p1
REM execute the class by calling its name without file extension
start java %~n1
The double-clickable JAR solution is the most common plain Java distribution method. There'd be a number of issues with trying to execute .class files directly, with the classpath the one that pops first to mind.
That said, if you wanted to support the very simplest possibilities in your development environment, you could conceivably implement a script that
inspected the .class file for the full class name (including package and inner class name)
walked up the directory tree to the root of the file's class path
(optionally included any common lib directories in the classpath)
invoked Java for the determined class
Then you could register your shiny script as a handler for .class files. But since you're in the development environment, aren't you happier with your IDE doing that?
For a .class file to run, needs in first place to have "something" to do, that is, that .class should contain a main method. Not all the .class do have one.
One thing you can do, is to wrap your app ( a number of .class files ) inside a jar file.
For short, you just need in addition to your classes a manifest file that says, where the main method is:
jar -cmf yourmanifestfile.mf doubleClickApp.jar *.class
And that's it, the doubleClickApp.jar is now executable with a "doubleClick" gesture.
When you install the Java Runtime Environment, it registers .jar files as an association in Windows. If you double-click on a .jar file, it will open it using Java. For this to work, you need to make sure you have a manifest defined that points to the class to run. Your class file to be run must have a main method that will be called.
Let's assume you have a class named 'com.TheClass.class' on disk. If you want to have this able to run with double click, create a file in a new directory called META-INF/manifest.mf. Put this into it:
Manifest-Version: 1.2
Main-Class: com.TheClass
Zip (or use the jar command) both your class up with this manifest directory and file. Rename it to mine.jar. Double click on it and it should launch your class with the Java runtime.
http://justaddhotwater.webs.com/javaexec.htm
This software makes it possible to run your Java classes by double-clicking them.(Windows ONLY).
The easiest way that I have found was creating a shortcut on the same folder than the .class file. Then right click on it and go to properties. Change the field Target to java NameOfClass, finally double click the shortcut :)