SciTE Java run subpackages - java

I'm using SciTE to write a small amount of Java at the moment. Only stuff to learn the language basics etc. At the moment I have a base directory, Java, a sub directory, practice, which has 2 subdirectories, running & testing. When I use the "build" command in SciTE I've managed to set that up to work. But what I cannot get working is the Java\practice\testing\ModelTester class (practice.testing package) to run. I get a NoClassDefFoundException and a wrong name message.
I know that the problem is the I need to run:
java practice.testing.ModelTester
but how do I get this into the command.go.*.java section of the SciTEDirectory.properties (in the Java directory). Obviously I would like something to put in command.go.*.java that can apply to any and every Java file I might run in a package.

Related

Call java file using relative path in bash shell

bash
/Desktop/Lab 3$ cd Stemmer
/Desktop/Lab 3/Stemmer$ java Stemmer
/Desktop/Lab 3/Stemmer$ cd ..
/Desktop/Lab 3$ java Stemmer/Stemmer
Error: Could not find or load main class Stemmer.Stemmer
Caused by: java.lang.NoClassDefFoundError: Stemmer (wrong name: Stemmer/Stemmer)
/Desktop/Lab 3$
Why doesn't java run Stemmer when I specify a relative path?
It seems to have run the program when I was in the directory, but I want java to run Stemmer as it did when I was in the directory that it was located in.
Could someone explain what is happening here please.
When you run a Java program, you specify the class name including the fully specified package, not the file path. This means that you separate the package "path" with dots, not slash. If you compiled a file named Stemmer.java that is in a folder named Stemmer and has package Stemmer as the first line, then you do
java Stemmer.Stemmer
There are 2 broad options:
You are trying to write an application in java
Then you need an IDE to develop it in, and a build system to produce the distributables, in the form of a jar file. There is no point or reason to trying to run this 'on the command line' the way you are. You need packages and a project definition.
You are just toying around for now, writing some basic code in a single source file.
Then just run the source file. This is a feature introduced in, I think, java11. Before that, this model of writing (stuff some lines in a source file and run it right away) is not what java itself is good at, only IDEs do that properly.
Starting with java11:
java Stemmer/Stemmer.java
works great, and no need to (re)compile anything. java will take care of it.
Explanation
When you run a Java program, you specify the class name including the fully specified package, not the file path.
You read this answer before, and then proceeded to completely ignore it and try java Stemmer.Stemmer which obviously doesn't work.
The class you have is named Stemmer, and it is in the unnamed package. Thus, to run it, java Stemmer is how to do this. It's not a file name. Stemmer.Stemmer is not java-ese for 'run the class file Stemmer in the subdirectory Stemmer, and 'package' is not java-ese for 'directory on the filesystem'.
The classpath root for your Stemmer class is its own directory, as you are not using any packages. the default classpath is the current directory. It is not possible to run the Stemmer class file without having its root on the classpath, so if /Desktop/Lab 3/Stemmer is not on the cp, you can't do it. So let's fix this:
java -cp '/Desktop/Lab 3/Stemmer' Stemmer
and that'll work fine.
More generally, using the unnamed package is a bad idea, and trying to run raw class files is similarly a bad idea - use an IDE for development, and a build system to build projects.
These rules and caveats all make perfect sense when writing a 'real' project (one you check into source control, and eventually deploy someplace or ship as a product to other users). But it's onerous and a tad ridiculous if just messing around. That's exactly why (these days) you can just specify a path to a java source file, which seems to be what you want to do. So, do that.

Is there a clean way to fix files path issue whether the program is launched from console or Eclipse?

I'm currently working on a Java project in which I have to open files I stored in a data directory next to src.
When I launch my program from Eclipse, to access these files: I type "data/fileName" whereas when I use console I have to type "../data/fileName".
(I couldn't manage to execute java src/Main from project directory and got the error :
Error: Could not find or load main class src.Main.java
Caused by: java.lang.ClassNotFoundException: src.Main.java )
Is there a way to make my program runnable on both console and Eclipse?
To give some context: I usually intended to run only on Eclipse but I encountered issues launching nano from Eclipse ($TERM variable , redirecting pipes to dev/tty also) so console execution has become a requirement.
I can't change my Eclipse configuration since this project won't be run on my computer and if it doesn't run because of default Eclipse settings, well...
Thank you for your replies.
Cordially.
Yes. Don't put those files there. There are 2 broad categories:
Read-only data that is as much a part of your app as your class files are. Think 'list of US states for the 'state' dropdown', 'an icon png to be shown on my user interface', 'a file containing a bunch of SQL to be used to initialize an empty database', and more.
For this, the proper way to go is YourClass.class.getResourceAsStream("foo.txt"), which will give you an input stream for reading the file "foo.txt", which is in the same place "YourClass.class" is. Even if it is in a jar file, for example.
This does mean that as part of your build, these files need to be in the jar. eclipse's limited build system does this. If you level up as a programmer and use a build system (you should be levelling up here), put them in src/main/resources and the build system should take care of it.
Or, the second category: Files that the user is supposed to edit, or files that need to be modified by your app.
These don't go anywhere near your jar file; executable and user-editable stuff should be nowhere near each other. These should be in the user home dir, you can fetch it with System.getProperty("user.home").
Note that to run java stuff, 'src' shouldn't be part of the story at all, java can't run source files, only class files. Also, the argument isn't a dir, it's a class name. java -cp path/to/your/app.jar com.foo.pkg.YourClass is how you run java code, not java src/Main.

How to use clojure files dynamically?

I want use java code to run some clojure files dynamically which are in some zip files.
If the clj.p1.core.clj is on the class path, it can runs correctly.
require.invoke(Clojure.read("clj.p1.core"));
How to make it dynamically?That is, put clj.p1.core.clj in the a1.zip (maybe some files), the java program could select the zip and then run it?
Probably, you should unzip those files first and then specify a *.clj file when invoking Compile class; take a look at its sources.
What would be much better in your case is to compile a Java class from Clojure sources first and then load that class in Java as well. Just add a specific step into your build process that cares of it. In that case, your Java code will look much simpler and wont' waste time on loading Clojure code dynamically.
Creating a Java file would be easy; just wrap Clojure sources with additional namespace with gen-class declaration. Move its output into your Java project or specify classpath properly. See gen-class page for more examples.

Class file creation concepts

I tried a strange experiment.
I created a project in Eclipse. I created Abc.java which has a main method which prints "Hello" to output.
I then built the project using eclipse. A class file named Abc.class was created. I copied that class file to some random location. Then , I navigated to the class file and used the following command to execute it.
java Abc
It printed "Hello" on the console. So far so well. I assume for a simple sysout there are no dependencies a JVM needs to resolve.
Next I created a very complex program in my eclipse for which I had to include 15 different jars(ex slf4j and apache-commons). After building the program in eclipse, I just copied its class files to a different location(not the JARS).
The main method which does all the complex coding was still in Abc.
I hit the command again(this time I followed package structure so I had to call a slightly different command).
java com.great.Abc
I was under the impression that since I hadn't added any jars to classpath in the java command, this code would break down miserably(remember it had a lot of dependencies).
However, its working absolutely fine.
Can someone please explain why?
(Half hour later :|)
Meanwhile I tried another experiment, and this amazes me even more. I mailed all the class files generated through eclipse to a different computer (note that I did not mail the jar files, only the class files generated by eclipse).
And I ran the program over there, hoping that it would break this time.
And guess what, it runs perfectly. Any inputs?
Are the jars required only at compile time?
How would I ensure that the jars I use are needed at runtime while creating a program?
Please explain calmly no matter how stupid the questions seem. :)
In Java, code dependencies are established between classes so when you create a class which just prints a message, your eclipse project setup has no relevance as long as your class is not using any class from the fancy eclipse setup.
Even if your class is referring to other classes it might happen that these classes are not loaded at runtime when the class is not really using them.
Even if you launch a Java program from a jar file which has dependencies to other jar files declared in its manifest, the standard Java launcher will ignore the absence of one or more of these jar files silently and during the program execution it will report failures only if required classes from these missing jar were tried to load.
One case were it is guaranteed that your program will break early is when its superclass is not present at runtime.
The Jar files aside, I actually find it strange enough that it even loaded your Abc class itself, since the JVM would look for com.great.Abc only in a com/great subdirectory of your classpath. I suspect that you, somehow, have some implicit classpath set in your environment or something, which points back to your Eclipse environment, and that the JVM found it there.
To verify this being or not being the case, you can run Java with the -verbose:class option, to make it tell you where it loads classes from:
java -verbose:class com.great.Abc
Eclipse uses the Absolute path to your included jar files.
Even when you move your main class it still works fine

Create Java Class from XSD at run time

I have a requirement in which my front-end screen creates an XSD at run time.
After which i need to create Java Classes for the same then zip it into an war file.
all this needs to be done at run time and within JVM.
I have researched on this and found that xjc( xjc -p foo myschema.xsd ) tool command. Unfortunately it seems that the tool cannot be run from inside my java application.
There is an Process API to run the tool from Java, but i think it will make it OS dependent.
Please help me.
I need to generated Java Source Code or direct Binaries(Class Files) from XSD at run time and package the same to an EAR.
You can definitely run the tool from your Java program, just like any other application can be invoked that way.
You can even supply the current working directory when executing the Process.
As long as you don't do anything OS dependent, then you won't make your application OS dependent. This is particularly relevant when supplying the paths. If they are relative, then use Linux style paths (and avoid spaces in the paths) to stay safe.
Once you do that, you can group the *.java and *.class files and Jar them using regular Java tools. From there, you can use some API to load the EAR into your Java EE environment.

Categories