Error Could Not Find or Load Main Class Twitter4j - javac

So I have a folder. Inside this folder is a jar file(twitter4j-core-4.04.jar) that my java file(Runner.java) imports and uses like so:
package twitter4j;
import twitter4j.Twitter;
import twitter4j.StatusUpdate;
import twitter4j.Status;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
I have all the right code -- a properly formatted:
public class Runner
and a proper main function --
public static void main(String[] args)
I compiled Runner.java using the following command and it compiled without error or warning: javac -cp .:twitter4j-core-4.0.4.jar Runner.java
Yet, when I go to run the class file it produced(using: java Runner) I get the error listed in the title. This error also occurs if I run the java command with the cp flag(Ive looked everywhere and used every possible classpath to no avail)
how can I fix this?

You have to create a runnable .jar file including your code dependencies like twitter4j-core-4.0.4.jar and (possibly) others that this depends on. If you are not using Maven, your IDE probably is capable of generating this kind of .jar file for you.

You must specify the class with its package.
This command must be executed from outside of folder twitter4j:
java -cp blabla.jar twitter4j.Runner

Right Click on class > Run as > Run Configuration
remove main class reference
Apply > Close
Now again right click on class > run as java application.
It worked for me.

Related

How compile uic with PySide6

I want to compile uic to PySide6 but I don't find how to install pyside6-uic tool. Where can I install pyside6-uic? I downloaded PySide6 but command pyside6-uic doesn't work.
There is a reference here in the title:
https://doc.qt.io/qtforpython/tutorials/basictutorial/uifiles.html#using-ui-files-from-designer-or-qtcreator-with-quiloader-and-pyside6-uic
Step 1. If you installed PySide6
a. In a venv then go to your venv's folder.
b. Globally then go to your python installation folder.
Step 2. Go to Lib then site-packages then PySide6.
Step 3. Copy uic.exe ( or uic if the file extension are hidden) create a folder called bin and paste what you've copied inside it.
To compile ui files from:
QtDesigner: From the top menu select Form -> View Python Code... then click on the save icon (floppy disk) from the newly opened window.
Command Prompt: pyside6-uic.exe mainwindow.ui > ui_mainwindow.py
PowerShell: pyside6-uic.exe mainwindow.ui -o ui_mainwindow.py
If you using virtual environment, when you install pyside6 with pip in the virtual environment there is a folder named Scripts there is the pyside6-uic.exe tool.
if you have install pyside6 globally in your system and you use visual studio code you can use the extension PySide2-vsc then when you installed you can go to preferences > settings and search the PySide2-vsc extension settings then look for the "Command to compile a .ui file into python". Then you can use that feature with right-click on .ui files.
I could solve this issue to add this Path to my %PATH% Variable on windows.
C:\Users\<YOUR_USER_PATH_NAME>\AppData\Roaming\Python\Python311\Scripts
The pyside6-uic tool is supposed to be installed automatically when installing the Python package.
Check if uic is in PATH
When using loadUiType, the Qt documentation (here) states that :
The internal process relies on uic being in the PATH. The pyside6-uic
wrapper uses a shipped uic that is located in the
site-packages/PySide6/uic, so PATH needs to be updated to use that if
there is no uic in the system.
But even then, I got the following error :
Cannot run 'pyside6-uic': "execvp: No such file or directory" -
Exit status QProcess::NormalExit ( 255 )
Check if 'pyside6-uic' is in PATH
For me, pyside6-uic was not located in site-packages/PySide6/uic. When reinstalling the module with pip, I noticed this message :
WARNING: The scripts pyside6-assistant, pyside6-designer, pyside6-genpyi,
pyside6-linguist, pyside6-lrelease, pyside6-lupdate, pyside6-rcc and pyside6-uic are
installed in '/Users/<user>/Library/Python/3.8/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning,
use --no-warn-script-location.
So make sure to add the right directory to your $PATH variable.
Once it's done, you will be able to use the pyside6-uic command to generate a Python class from a UI file :
pyside6-uic mainwindow.ui > ui_mainwindow.py
Loading a .ui from code
You can also load a .ui file from your code using either:
loadUiType (doc page) :
This function generates and loads a .ui file at runtime, and it
returns a tuple containing the reference to the Python class, and the
base class.
or QUiLoader (doc page):
enables standalone applications to dynamically create user interfaces
at run-time using the information stored in UI files or specified in
plugin paths
from PySide6.QtUiTools import QUiLoader
ui_file = QFile("mainwindow.ui")
ui_file.open(QFile.ReadOnly)
loader = QUiLoader()
window = loader.load(ui_file)
window.show()
Generally speaking, use absolute paths to access your UI files. Relative paths are susceptible to errors.

Why while compiling java code through cmd it's written as filename.java and running the same file we omit filetype?

While compiling java files through cmd (ie through javac command) it is written as filename.java but to run the same program we write filename and don't add the extension. Why is extension removed in java command or extension added in javac command ? Any specific reason.
When compiling, you specify the name of one or more source files, thus the .java extension
When running you specify the class name of the main class, thus no extension.
The difference becomes even more apparent, when the class is in a package:
javac mypackage/MyClass.java
java mypackage.MyClass

Dart pub global run can't resolve script name

I have a simple dart project that has one executable in bin folder (test.dart). I have activated it with dart global activate and now I can run it directly with just typing the name of that executable file.
Inside that dart file I would like to know the path of that script. Basically for now I'm just printing something like this:
print('1: ' + Platform.script.toString());
print('2: ' + Platform.script.path);
print('3: ' + Platform.executable);
print('4: ' + Platform.packageRoot);
print('5: ' + Platform.resolvedExecutable);
When I run it directly:
test
or with pub:
pub global run test
or even with package name:
pub global run test:test
I always get the same result:
1: http://localhost:53783/test.dart
2: /test.dart
3: E:\apps\dart-sdk\bin\dart
4:
5: E:\apps\dart-sdk\bin\dart.exe
The issue here is that I can't get the absolute path for test.dart file.
When I run it like this:
dart /path/to/project/bin/test.dart
I get what I need:
1: file:///E:/projects/dart/test/bin/test.dart
2: /E:/projects/dart/test/bin/test.dart
3: dart
4:
5: E:\apps\dart-sdk\bin\dart.exe
Is there a way how to get absolute path for a script that is currently running, regardless of a way how it was executed?
tl;dr: There's not a great way of doing what you want, but it's in the works.
The notion of a "path for a script that is currently running" is more complicated than it might sound at first blush. There are a number of ways that the Dart ecosystem invokes main(). Off the top of my head, here are a few:
Manually running the file with dart.
Running the file in an isolate.
Compiling the file to a snapshot and either manually running it or running it in an isolate.
Automatically compiling the file to a cached executable and running that either in a subprocess or in an isolate.
Adding a wrapper script that imports the file and invokes main(), and either running that in a subprocess or in an isolate.
Serving the file over HTTP, and running it either in a subprocess or in an isolate.
In some of these cases, the "script that is running" is actually a wrapper, not the original file you authored. In others, it's a snapshot that may have no inherent knowledge of the file from which it was created. In others, the file has been modified by transformers and the actual Dart code that's running isn't on disk at all.
I suspect what you're actually looking for isn't the executable URL itself, but the location of your package's files. We're working on a collection of APIs that will make this possible, as well as a resource package that will provide a nice API for dealing with your packages' resources.
Until that lands, there is a dart:mirrors hack you can use. If you give one of your library files an explicit library tag, say library my.cool.package, you can write:
var libPath = currentMirrorSystem().findLibrary(#my.cool.package).uri.path;
This will give you the path to your library, which you can use to figure out where your package's lib/ files live.
If you want a reliable way to access the current file while running in pub you'll need to use mirrors.
Here's a sample usage with dartdoc tests - https://github.com/dart-lang/dartdoc/blob/41a5e4d3f6e0084a9bc2af80546da331789f410d/test/compare_output_test.dart#L17
import 'dart:mirrors';
Uri get _currentFileUri =>
(reflect(main) as ClosureMirror).function.location.sourceUri;
void main() { ... }
Not particularly pretty, but it's easy to just put into a util file.

How do I get the script path in Dart?

I need to find out the directory in which the Dart script (which is currently running) is located. How do I go about doing this?
Previously, this was possible through new Options().script, but that class is no more. The arguments list from main(args) doesn't help, since it only contains arguments, not the executed Dart file (at least on Mac OS X).
According to BREAKING CHANGE: dart:io Platform and Options are deprecated, and will be removed and Breaking Change: Platform.script is a Uri, dart:platform library is gone new Options().script should be replaced by Platform.script.
Note that Platform.script returns an Uri and you should consider using toFilePath to get the file path from the file URI.
As noted the simple method is:
import 'dart:io';
print(Platform().script);
> file:///home/..../dcli_unit_tester/bin/dcli_unit_tester.dart
This is a file url, so to get the actual path use:
import 'dart:io';
print(Platform().script.toFilePath());
Of course you wanted the directory rather than the script so:
import 'dart:io';
import 'package:path/path.dart';
final pathToScript = Platform().script.toFilePath();
final pathToDirectory = dirname(pathToScript);
print(pathToDirectory);
> /home/..../dcli_unit_tester/bin
There are a few things you should understand when attempting to get the directory of the script in that the directory will change depending on how you run the script.
In particular if running your script from a globally activated package then you might not (should not) want to use the script's dirirectory for storing things.
execute .dart from cli
Here is the output from Platform if you run it from the cli:
dart bin/dcli_unit_tester.dart -p
executable: dart
script: file:///home/..../dcli_unit_tester/bin/dcli_unit_tester.dart
execute script when globally activated
Now if you run the same script from a globally activated package:
pub global activate dcli_unit_tester
dcli_unit_tester -p
executable: dart
script: file:///home/..../.pub-cache/global_packages/dcli_unit_tester/bin/dcli_unit_tester.dart-2.14.2.snapshot
In this case the directory is in pub-cache which by nature is transitory, so don't store anything that you want to get back later.
execute from compiled script
Now if you compile the script:
dart compile exe bin/dcli_unit_tester.dart
bin/dcli_unit_tester.exe -p
executable: bin/dcli_unit_tester.exe
script: file:///home/..../dcli_unit_tester/bin/dcli_unit_tester.exe
execute from within a unit test
If you run the same test from a unit test the script will be the test.dll file not you script.
I will update this answer once I work out how to get the script directory when running under unit tests (if it is even possible).
using dcli
If you are building a console app you can use the dcli package:
import 'package:dcli/dcli.dart'
print(DartScript.self.pathToScript);
print(DartScript.self.pathToScriptDirectory);
> /home/..../dcli_unit_tester/bin/dcli_unit_tester.dart
> /home/..../dcli_unit_tester/bin

javac command is not able to find .class file in the current directory needed to compile a source file

The files A.java and B.class(Bytecode version of B.java) are in the current directory.
A.java uses B.java in the following way:
class A {
B b;
}
From what I have read the JDK tools first look in the directories where the Java standard libraries are installed. If the class is not found in the standard libraries, the tool searches in the class path. When no class path is defined, the default value of the class path is assumed to be the current directory. Then why is the following command not working:
C:\current> javac A.java
The package structure must match the directory structure, otherwise javac will fail.
http://kevinboone.net/classpath.html
Comment out or get rid of the package statements in the begin of your classes. Since you do keep both java classes in the directory where you compile, the compiler should be able to find the B.class without trouble.

Resources